#### Archived

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

# Floating point rand values - How?

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

## 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 on other sites
I dont know if this is what you mean: Use rand() with a number like rand()%100, then divide by 100 or 1000, to get a decimal number.

-J

##### 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 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 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 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 on other sites
Ok, I stand corrected

##### Share on other sites
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 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

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009406
• ### Who's Online (See full list)

There are no registered users currently online

×