Archived

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

Signed random number

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