Generating Specific Random Numbers

Jan 15, 2011 at 5:16pm
I need to generate a specific set of random numbers. For instance,I need a random number between 4-7. I understand how to generate a random number from 0 - what ever, but do not know how to generate a specific set of random numbers from any integer range such as the example above(4-7). Please help if you can, thanks.
Jan 15, 2011 at 5:24pm
you could get the diff 7-4 = 3, and get a random number 0-3... int x = rand()%4 (after you seed of course. Then you just adjust it like so: x+4, then the number will be between 4 and 7

see http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ for more
Last edited on Jan 15, 2011 at 5:25pm
Jan 15, 2011 at 5:36pm
lol, it amazes me how when given the answer, it is some thing that you feel is quite simple and you should have thought of. Thanks for your help.
Jan 15, 2011 at 5:38pm
np, if you haven't done it in a while, the brain gets rusty..
Topic archived. No new replies allowed.