# Thread: how to generate random number between two numbers?

1. ## how to generate random number between two numbers?

How can I generate a random number between two numbers using stdlib?

printf("%d\n", rand() % 50); generates a number between 0 and 49, but how can I generate numbers between 10 and 20 or -10 and 10 with and without decimals? Is it possible with rand() ? 2. Yes, it is possible, and you basically asked this in a previous post.

Feel free to peruse the further discussion in that post, but as I noted if you care about the quality of your PRNG, you should use a specific PRNG, not rand(). If you are satisfied with rand() despite its loose requirements, then the (rand() % (n - m + 1) + m) approach to generate a random integer in the range [m, n] is fine. 3. Remember that a random number between 10 and 20 is the same as a random number between 0 and 10 plus 10. i.e. Get a random number between 0 and 10 and add 10 to it Popular pages Recent additions generate, number, numbers, rand, random 