Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

peck

Floating point rand values - How?

This topic is 5794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need help with a method of finding a random float between two given float values, either of which may be negative or posotive. I apologise if this has been asked before, i had a look but couldnt find anything. [edited by - peck on September 8, 2002 7:58:21 AM]

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Fruny
double min, max;

double num = min*( (double)rand()/(double)RAND_MAX)* (max-min) );


The beginning should be min +, not min *

double num = min+ ( (double)rand()/(double)RAND_MAX)* (max-min) );

Cédric

Share this post


Link to post
Share on other sites
it''s often quicker to get an int rand than a float (especially if you write your own) then multiply it by the constant 1/4294967295 to get a number of size less than 1. multiply this by the gap size and add the low value

********


A Problem Worthy of Attack
Proves It''s Worth by Fighting Back

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
double num = min+ ( (double)rand()/(double)RAND_MAX)* (max-min) );

And I believe that x/y where y is a constant, won't be optimized as x*(1/y) because it leads to slightly different results. So that needs to be done explicitely to gain some speed (float/double mul is much faster than div)

double num = min + (rand() * (1.0 / RAND_MAX)) * (max - min);

(IIRC the (double) casts are useless, since ints are automatically casted to doubles if the other operand is a double)

[edited by - civguy on September 8, 2002 12:47:47 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by civguy
And I believe that x/y where y is a constant, won''t be optimized as x*(1/y) because it leads to slightly different results. So that needs to be done explicitely to gain some speed (float/double mul is much faster than div)

double num = min + (rand() * (1.0 / RAND_MAX)) * (max - min);



I think it is optimized, at least in VC.NET. Take a look at this ASM output. Doesn''t seem to be an fdiv in there.


; 43 : double min = 0, max = 10;
; 44 : double num1 = min + ((double)rand() / (double)RAND_MAX) * (max-min);

call _rand
mov DWORD PTR tv280[esp+32], eax
fild DWORD PTR tv280[esp+32]
fmul QWORD PTR __real@3f00002000400080
fmul QWORD PTR __real@4024000000000000
fstp QWORD PTR _num1$[esp+32]

; 45 : double num2 = min + ((double)rand() * (1.0/(double)RAND_MAX)) * (max-min);

call _rand
mov DWORD PTR tv278[esp+32], eax
fild DWORD PTR tv278[esp+32]
fmul QWORD PTR __real@3f00002000400080
fmul QWORD PTR __real@4024000000000000
fstp QWORD PTR _num2$[esp+32]



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Even if division is faster, how can this:

double num = min + (rand() * (1.0 / RAND_MAX)) * (max - min);

be faster than this:

double num = min + (rand() / RAND_MAX) * (max - min);

when the first one has to do an extra operation, the multiplication.

Share this post


Link to post
Share on other sites
1.0 / RAND_MAX can be precalculated, because RAND_MAX is a constant, so the ''intermediary'' result is:

x * k1

vs.

x / k2

If multiplications are faster, then the first one will be faster. However, the compiler knows that, so it automatically evaluates 1 / k2 to use a multiplication.

Bottom line: don''t micro-optimize because the compiler knows better than you.

Cédric

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!