Jump to content
  • Advertisement

Archived

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

evolutional

Signed random number

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

Is there a quicky and easy way to generate a signed random number? I know how to make a random between a positive interval, no problem, but what if I want the sign to be random too? Is there a quicker way than doing this: (Pseudocode) Generate random number -> r; Generate random number between 0 and 1 -> s; If s < 0.5 then return 0 - r else return r

Share this post


Link to post
Share on other sites
Advertisement
int signedRand = rand() - RAND_MAX/2;

i.e. just substract from the random number half of it''s maximum value

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!