#### Archived

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

# Random number between 2 bounds.

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

## Recommended Posts

Hey. I''ve found myself needing to get a number between 2 bounds, IE 0 and 1, or 0 and 255. I know I can do a integer result with rand() and the modulus operator, but Id really like a floating point solution. Whats worked for you guys ? From what Ive seen of perlin noise its really slow. Other random formulae I really dont know of. :-/ I cant recall ever running across this problem in my various studies. Oh yeah- ideally its prety fast- its going to be for a particle emitter. Thanks, ~V''lion Bugle4d

##### Share on other sites
#define RANDOM_FLOAT (rand()/RAND_MAX)
#define RandomNumber(a) (RANDOM_FLOAT*a)
#define RandomRange(a,b) (RandomNumber(b-a)+a)

RANDOM_FLOAT will give you a random number between 0 and 1, RandomNumber will give you a random floating point between 0 and a, and RandomRange will give you a random number between a and b.

Hope this helps!

"There are only three types of people in this world: those who can count, and those who can't."

Just3D
Justin Nordin
J Squared Productions
www.jsquaredproductions.com

[edited by - Just3D on July 27, 2002 6:19:49 PM]

[edited by - Just3D on July 27, 2002 6:20:41 PM]

[edited by - Just3D on July 27, 2002 6:22:08 PM]

##### Share on other sites
Just3d,

Those macros should probably read as follows:

#define RANDOM_FLOAT ((float)rand()/(float)RAND_MAX)
#define RandomNumber(a) (RANDOM_FLOAT*a)

##### Share on other sites
Tsk..tsk.. so many edits...next time I''ll try and proof-read my message.

##### Share on other sites
(doh!)

Yeah, forgot the casts. (Can''t be too careful).

##### Share on other sites
What if you want to find a random # that is not completely random? for instance, what if you want the lower #s to occur more often. In essence, how can you generate a random #, n, where the frequency of the a number = 1 / number ^ 2 or somethign similar... is there a way to do this using a funcion for the frequency f(n)?

thanx for the help!

tazzel3d ~ dwiel

##### Share on other sites
Well, if you square the random number before dividing by RAND_MAX², there should be more small numbers than large numbers.

Cédric

##### Share on other sites
My random number generator returns a truly random number between two bounds. Generating biased random numbers would be a bit harder.

##### Share on other sites
quote:
Original post by cedricl
Well, if you square the random number before dividing by RAND_MAX², there should be more small numbers than large numbers.

Warning - RAND_MAX squared might roll-over on some implementations. Might want to do the division first and square the result.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

##### Share on other sites
why didn't I think of it to start with!?!?!?! all you hav to do to generate biased random numbers is create a function to pass the random number through... example:

    #define RANDOM_FLOAT ((float)rand()/(float)RAND_MAX)// biases a random # between 0 and 1float BiasNumber(float num){    return num * num;}// scales the biased # between the range of min and maxfloat ScaleNumber(float num, float min, float max){    return min + (max - min) * num;}// to generate the number, execute:float min = 0;float max = 1000;float randomnum = ScaleNumber(BiasNumber(RANDOM_FLOAT, min, max);  `

of course this could all be put into one function, but for ease of explantaion, I wrote it as two. In this way, you can bias the number any way you wish as long as the number always stays between the range of 0 and 1.

if anyone else can come up with a way to bias the number or can pick my method apart, please do so!

Tazzel3d ~ dwiel

[edited by - tazzel3d on July 27, 2002 10:27:30 PM]

1. 1
2. 2
3. 3
Rutin
20
4. 4
khawk
14
5. 5

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013187
×