Random numbers generation problem
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
How to create randi([a,b],1,c) in which every number is not divided by 2,3,5,7,11 ? Or not neccecary in randi, it can be other function. I should create a row of random numbers in a specific interval that are not divided by 2,3,5,7,11.
0 commentaires
Réponses (2)
David Goodmanson
le 13 Oct 2022
Modifié(e) : David Goodmanson
le 13 Oct 2022
Hi Daniel,
here is one way, basically the sieve of Eratosthenes.
nmax = 1000;
a = randi(nmax,1,1e6);
a(rem(a,2)== 0) = [];
a(rem(a,3)== 0) = [];
a(rem(a,5)== 0) = [];
a(rem(a,7)== 0) = [];
a(rem(a,11)== 0) = [];
0 commentaires
John D'Errico
le 13 Oct 2022
Modifié(e) : John D'Errico
le 13 Oct 2022
Just compute the set of all numbers in the interval you care about that are NOT divisible by those small primes. Technically, the numbers you care about are called rough numbers. Here, they would be called 13-rough, as the candidates you care about are divisible by no integer smaller than 13. (The smallest 13-rough number is 13., then 17 is next, etc. The smallest composite 13-rough number is 169=13^2.) But generating the list you want is simple. David shows how.
R = 100:1000;
R(mod(R,2) == 0 | mod(R,3) == 0 | mod(R,5) == 0 |mod(R,7) == 0 |mod(R,11) == 0) = [];
numel(R)
So there are 186 13-rough numbers between 100 and 1000. Remember that all primes greater than 11 are 13-rough.
Then sample randomly from that set. How would you sample randomly from a set of say 50 arbitrary integers? You use randi to generate random integers from 1 to 186 (in this case), then use them as indices into the set of interest.
0 commentaires
Voir également
Catégories
En savoir plus sur Random Number Generation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!