
Random number between 2 bounds.
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 I`d really like a floating point solution.
What`s worked for you guys ?
From what I`ve seen of perlin noise its really slow.
Other random formulae I really don`t know of. :-/
I can`t 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

~V'lionBugle4d
#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]
#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]
"There are only three types of people in this world: those who can count, and those who can't."Just3DJustin NordinJ Squared Productionswww.jsquaredproductions.com
July 27, 2002 05:22 PM
Just3d,
Those macros should probably read as follows:
#define RANDOM_FLOAT ((float)rand()/(float)RAND_MAX)
#define RandomNumber(a) (RANDOM_FLOAT*a)
Those macros should probably read as follows:
#define RANDOM_FLOAT ((float)rand()/(float)RAND_MAX)
#define RandomNumber(a) (RANDOM_FLOAT*a)
Tsk..tsk.. so many edits...next time I''ll try and proof-read my message.
"There are only three types of people in this world: those who can count, and those who can't."Just3DJustin NordinJ Squared Productionswww.jsquaredproductions.com
(doh!)
Yeah, forgot the casts. (Can''t be too careful).
Yeah, forgot the casts. (Can''t be too careful).
"There are only three types of people in this world: those who can count, and those who can't."Just3DJustin NordinJ Squared Productionswww.jsquaredproductions.com
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
thanx for the help!
tazzel3d ~ dwiel
Well, if you square the random number before dividing by RAND_MAX², there should be more small numbers than large numbers.
Cédric
Cédric
My random number generator returns a truly random number between two bounds. Generating biased random numbers would be a bit harder.
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 ]
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:
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]
#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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement