Archived

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

Floating point rand values - How?

This topic is 5580 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
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
quote:
Original post by 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.
Because RAND_MAX is a constant, and 1.0/RAND_MAX always leads to same result, so it can be safely precalculated by the compiler. Leading to (rand() * 0.0000000314159265358979) or something.

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
Bottom line: don't micro-optimize because the compiler knows better than you.
I beg to differ. There are some 'unsafe' operations that you can optimize better than the compiler can, since self-made optimizations may lead to slightly different results, but still unnoticeable in real applications.

For example, take the good old days when sin() and cos() were costly operations. Using sin-table could've boosted your application's inner loops dramatically but sin-table still was nothing but micro-optimization. So I dare you to show me a compiler that can do sin-tables itself as you claim compiler knows better than me .

Here I just (falsely) assumed that division would lead to different results than multiplying with 1/x, which would've disabled compiler's ability to make that decision.

[edited by - civguy on September 8, 2002 2:55:59 PM]

Share this post


Link to post
Share on other sites