Random numbers generation problem
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
David Goodmanson
el 13 de Oct. de 2022
Editada: David Goodmanson
el 13 de Oct. de 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 comentarios
John D'Errico
el 13 de Oct. de 2022
Editada: John D'Errico
el 13 de Oct. de 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 comentarios
Ver también
Categorías
Más información sobre Random Number Generation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!