Jump to content
  • Advertisement

Archived

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

Vlion

Random number between 2 bounds.

This topic is 5894 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

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

Share this post


Link to post
Share on other sites
Advertisement
#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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Just3d,

Those macros should probably read as follows:

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 1

float BiasNumber(float num)
{
return num * num;
}

// scales the biased # between the range of min and max

float 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]

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!