Rnd(0,1) creating higher numbers.

Blitz3D Forums/Blitz3D Programming/Rnd(0,1) creating higher numbers.

sting(Posted 2011) [#1]
Hello.

In my program Ive been using Rnd(0,1) to fill some floating vars inside a type with some random values from 0 to 1 for testing. This works fine 99% of the time but sometimes I catch it outputing something like "3.8147e-005". I was wondering if anyone else has had this prob and If its just a glitch in the rnd generator in B3D.

Also, before anyone asks, I am calling it like this:
Set_AI_Personality(tester, i, Rnd(0,1))

And setting it like this:
Function Set_AI_Personality(L_name, L_pe=99, L_g#=0.5)
x.AI=Object.AI(L_name)
Select L_pe
	Case 0
		x\Curiosity#=L_g#
;... ect.


Sorry if i don't post more code, It's rather long and full of irrelevant stuff.

Thanks.

Edit: Oh, and I am NOT seeding the Rnd in any way.. yet.
Edit 2: I just added:
If L_g# > 1.0 Then L_g# = 1.0
If L_g# < 0.0 Then L_g# = 0.0

because of its obveous fixing qualities... And It didn't help. So I'm stumped.

Last edited 2011

Last edited 2011


Yasha(Posted 2011) [#2]
"3.8147e-005" is scientific notation for 0.000038147. The function is working exactly correctly, just emitting a very, very small number, and using a compact representation when asked to display it.

If you want to force all the numbers to be larger (so this doesn't happen), there are a couple of ways; the simplest might be to just use Rand instead of Rnd, for instance generating an integer between 0 and 1000, then dividing by 1000.0 to put it back between 0 and 1. Doing this will also give you a fairer distribution of results, as these extremely small numbers will cluster towards the zero end if you use Rnd.


sting(Posted 2011) [#3]
Ah, I see now. Thank you very much Yasha.


_PJ_(Posted 2011) [#4]
"3.8147e-005" is scientific notation for 0.000038147

It's showing the number with a fixed decimal and exponent.
The exponent is the value followed by e and represents the power of 10 which the preceeding fraction should be multiplied by.

So

3.8147e-005 means 3.8147 * (10^(-5))

Multiplication by negative powers is equivalently dividing by the absolute magnitude of the power:

3.8147 / 10^5

3.8147*0.00001


sting(Posted 2011) [#5]
I did recall that that was scientific notation, though I guess I had thought that the floats blitz uses were limited to like.. 6 digits.

I just wanted to make sure that I wasn't reading some memory overflow or something.

Thank you all. :)


stanrol(Posted 2011) [#6]
good, it works finally.