How to create a set of random numbers
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Joy
el 30 de Mzo. de 2017
Comentada: Joy
el 31 de Mzo. de 2017
Hi, how I could generate random number 90 times
- probability space are 1, 2, 3
- totally 90 random numbers
- got 21 of 1, 38 of 2 and 31 of 3
Could you give me some solutions?
Thanks Joy
0 comentarios
Respuesta aceptada
Stephen23
el 30 de Mzo. de 2017
Editada: Stephen23
el 30 de Mzo. de 2017
You could use randperm to randomly arrange a vector of exactly those numbers:
>> vec = [repmat(1,21,1);repmat(2,38,1);repmat(3,31,1)]; % or use REPELEM
>> vec = vec(randperm(numel(vec)));
And checking that it fulfills your requirements:
>> numel(vec)
ans = 90
>> nnz(vec==1)
ans = 21
>> nnz(vec==2)
ans = 38
>> nnz(vec==3)
ans = 31
Más respuestas (0)
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!