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="Frank Silvermann, post: 2468782"] [i][i][i][i][i] Gosh, this opens up a whole new set of problems for me. In stddef.h for me I have: /* define the implementation dependent size types */ #ifndef _SIZE_T_DEFINED typedef unsigned int size_t; #define _SIZE_T_DEFINED #endif ,so I would think that making these calls with unsigned ints would be alright, but when I tried to do this with RandRange, my return is the same every time I run the resulting executable: #include <stdlib.h> #include <stdio.h> #include <time.h> extern size_t RandRange(size_t a, size_t b); /* attribution: CLR */ int main(void) { unsigned int a, b, c; srand(time(NULL)); a = 40; b = 90; c = RandRange( a, b); printf("return is %u\n", c); return 0; } /* C-FAQ */ size_t RandRange(size_t a, size_t b) { size_t c = (size_t) ((double) rand() / ((double) RAND_MAX + 1) * (b - a)); return c + a; } /* end source */ Furthermore, I don't see how the RHS of the assignment to c would give you a number between 'a' and 'b'. To make matters worse, I was unable to find enlightenment in the FAQs. I appreciate your help, but I'm still sunk. frank[/i][/i][/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
rand in a closed interval on the ints
Top