Generating random 20 number between 0.25 to 2.8 and counter odd numbers in 'array'?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
tomer kapuri
el 3 de Dic. de 2018
Comentada: tomer kapuri
el 3 de Dic. de 2018
Generating random 20 number between 0.25 to 2.8 and counter odd numbers in 'array'?
I can't do this :(
it's not working:
g=randi([0.25 2.8],1,20)
numberOfOddNumbers = sum(rem(g, 2))
0 comentarios
Respuesta aceptada
madhan ravi
el 3 de Dic. de 2018
Editada: madhan ravi
el 3 de Dic. de 2018
a = .25;
b = 2.8;
r = (b-a).*rand(1,20) + a; % note that your creating floating numbers
7 comentarios
Guillaume
el 3 de Dic. de 2018
The majority of random numbers between 0.25 and 2.8 don't have just three digits after the dot. In fact an infinite number of them have an infinite numbers of digits after the dot.
In the real of computers, among the 15,312,238,733,059,687 numbers than can be generated there's only 2552 numbers with 3 or less digits after the dot. Most of these 2552 numbers can't even be stored exactly as double.
Más respuestas (1)
Guillaume
el 3 de Dic. de 2018
Editada: Guillaume
el 3 de Dic. de 2018
It seems to me what you're actually asking is to generate integer numbers between 250 and 2800. These numbers can easily be tested to see whether they're odd or even.
You can then pretend that these integers are divided by 1000 to get numbers between 0.25 and and 2.8 with at most 3 digits. Note that actually performing the division won't get you numbers that are actually at most 3 digits due to the way numbers are stored on a computer. For example, the result 400 / 1000 is not stored as 0.4, it is stored exactly as 0.40000000000000002220446049250313080847263336181640625.
numbers = randi([250 2800], 1, 20); %numbers are implicitly divided by 1000
sumodd = sum(mod(numbers, 2)); %as explained by Steven Lord, that's a weird definition of even/odd
3 comentarios
Guillaume
el 3 de Dic. de 2018
1. See Madhan's answer
2. Easy enough
3. Now, we're back to talking about even/odd real numbers, which as explained by Steven Lord doesn't make any sense. If you're talking about fraction of 1000 numbers whose numerator is even or odd, then see my answer.
Nothing is particularly difficult as long as you use the same math as everybody else.
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!