### Thread: generating random number

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
28
Rep Power
0

#### generating random number

Hi,

Is there a function that can generate numbers within a specified range? I know that rand() generates numbers between
[0, RAND_MAX]. But what I want to know if there is one that can generate a number between [0, i].
2. try "rand() % i;". That will get you values from 0 to i-1.

Keep in mind that rand() is a very crappy random number generator!
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
28
Rep Power
0
Oh, I see. Is there another random number generator in the C lib which I could use to perform this task? Thanks for the reply.
4. If you wish to generate numbers from 0..2^n-1, where n=integer (for example, 0..15, 0..63, 0..255) then try: rand() & (2^n-1). This is much faster than %, although most compilers will optimize this, anyway.
5. To my knowledge, neither the C or C++ lib contains a built-in good random number generator. google for 'pseudo random number generator' and you should find tons of examples. It is also very important what you are using the numbers for. If you are doing encryption, you MUST use a crypto secure number generator or you will be introducing bias that can be easily used to decrypt the message. If you are using it for simulations you need to have a very long period between repeating sequences and a uniform distribution of numbers. There are some sources of true random numbers on the web, and you can also purchase hardware random number generators. Some OSs support a random number generation based on hardware responses, but those numbers are typically used as seeds for software random number generators.

What is your plan for your random numbers?
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
28
Rep Power
0
I filled an array A[i] = i + 1 and then in a for loop i want to swap the values randomly.

for( i=1;i<N;i++)
swap(&A[i], &A[randint(0,i)]);
7. I think the use of rand() will be fine for what you are doing (homework, I presume?).
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
28
Rep Power
0
Ya. I have to time how long it takes. Im new to C so Im not quite sure how to use these built in functions. Is it possible to specify the range of rand() and increment it as we move along or does it always have to be
[0, RAND_MAX]?
9. I would do something like this (didn't do a test compile, but ought to work):

Code:
```int randNum;

for( i=1;i<N;i++){
randNum = rand() % N;
swap(&A[i], &A[randNum]);
}```
Be sure to initialize rand() with srand(), I am not sure what happens if you don't do that. Besides, that way you can test it with various seeds to see what happens. The value returned by rand() is always 0 to RANDMAX, you can't change that unless you want to write your own version.
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
28
Rep Power
0
Great! Thanks a lot for the help!
11. Thank you guys!
12. Check out http://forums.devshed.com/t85630/s.html, they guy posted a random number generator you might want to try. Dunno if it is worth anything, but here it is.