Need a little help understanding this math
Blitz3D Forums/Blitz3D Beginners Area/Need a little help understanding this math
| ||
Unless I am crazy or something which could be the case...in the example below it shows the lowest and highest numbers get picked 50% of the time. why is that? math fluke? Graphics3D 800,600,0,2 SeedRnd MilliSecs() Repeat Cls vara=Rnd(1,5) If vara = 1 Then varb=varb+1 If vara = 2 Then varc=varc+1 If vara = 3 Then vard=vard+1 If vara = 4 Then vare=vare+1 If vara = 5 Then varf=varf+1 Text 1,1, "vara- the number picker: "+vara Text 1,12, "varb: "+varb Text 1,24, "varc: "+varc Text 1,36, "vard: "+vard Text 1,48, "vare: "+vare Text 1,60, "varf: "+varf UpdateWorld RenderWorld() Flip Until KeyHit (1) End EDIT: after looking, I replaced "Rnd" with "Rand" and it cleared up the problem...weird though ay'? Last edited 2011 Last edited 2011 |
| ||
I think, it is because Rnd() is floating point: 1.00 - 1.499 -> 1 1.50 - 2.499 -> 2 ..... 4.50 - 4.999 -> 5 and this means the range for getting "1" has a size of 0.5. And the range for getting "2" is 1.0. So "2", "3" and "4" are picked twice as often as "1" and "5" |
| ||
It is because rnd returns a floating point value, but you are storing them in integer variables. |
| ||
thx for the replies...makes sense :) cheers |