#### Archived

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

# Signed random number

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

## 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 on other sites
signedrandom( int max ) {

return( random( 2*max ) - max );
}

ToohrVyk

##### 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 on other sites
Duh, I tried everything but that.

Thanks very much to you both

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013596
×