Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
How do you guys limit rand() results to a range?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Keith Thompson, post: 5162922"] I think you're right. If RAND_MAX==32767 then each call gives you exactly 15 bits of randomness. In principle, you should be able to treat a sequence of results as a continuous stream of randomness from which you can extract random numbers in any range you choose, using exactly as many bits as you need (not necessarily a whole number) for each one. In practice, though, since we can only store and manipulate discrete values (even in floating-point), I think any method must still be vulnerable to an unlikely sequence that would cause it to break. I suppose you could use arbitrary-precision rational numbers to store the pending randomness, but then you lose performance and risk running out of memory. (I don't seriously suggest that as a solution.) [...] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
How do you guys limit rand() results to a range?
Top