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
rand in a closed interval on the ints
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Richard Heathfield, post: 2468875"] CBFalconer said: Only if b is greater than or equal to a, and provided you don't want b to be an allowable result unless it is 0. And why all those casts? And why the lousy names a and b? Here's a version which eschews the casts, fixes the names, and sorts out the problem of the parameters being in an inconvenient order. #include <stdlib.h> size_t RandRange(size_t low, size_t high) { if(low > high) { size_t t = low; low = high; high = t; } return (high - low + 1) * (rand() / (RAND_MAX + 1.0) + low; } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
rand in a closed interval on the ints
Top