Random number range in C

Asked

Viewed 31,819 times

11

In a certain program, I need to generate a random value greater than 1000 (with a limit set by me to up to 5000 (maybe it’s still too high)). This unique code refers to a type of registration to be performed. After answering some doubts of rand() % with a teacher, we come to this code: 1000 + rand() % rand().

Would it be enough for what I want? And how could I delimit it to up to 5000? This being the only part I could not implement.

  • 2

    I think it should be 1000 + rand() % 4000 but I haven’t tested.

3 answers

11


The function rand() generates pseudo-random numbers between 0 and (possibly) 32767 (depending on implementation may have greater amplitude).

1001 + ( rand() % 4000 ) //os parenteses estão aí só para deixar a precedência mais visível

You say you need number above 1000, so the smallest possible number the expression should generate is 1001. As the function rand() has as lower value the zero, just add 1001 to reach the lower limit considering that we are talking of integers.

To ensure that no number comes outside the range, we divide the function result by the amount of possible elements in the range. If it goes from 1001 to 5000, we have 4000 possible numbers in the desired range. So the division by 4000 will take from 0 to 3999 inclusive.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I understood better now how Rand() works, and its explanation was more than excellent. Thank you! :)

  • but the problem of this function that however random, it always returns the same sequence

  • @Joannis agree but then just adapt to use the time, I left almost ready. After I edit p/ stay the way you want.

  • I already did, if you want you can paste my reply in your!

  • the important thing is to help him =D

7

Complement to @Maniero’s reply

The function void srand (unsigned int seed)

  • Each element of a pseudo-random sequence is generated from the previous element.
  • How desirable it may be to repeat a pseudo-random sequence in C always uses the same first element.
  • The function void srand (unsigned int seed) allows you to vary this first element, which serves as seed of the sequence.

Using the watch as seed

  • The time library has a time function whose result is a number of seconds passed from a fixed moment (00:00:00 UTC of January 1, 1970).

  • This number can be used as seed in the call for srand to generate a variable and unpredictable seed.

    • The result of time there’s a guy time_t and must be converted into unsigned int so that it can be used as an argument for srand.

    • The function time also requires an argument that the effect of this application can be passed as null

    • srando((unsigned) time(NULL))

Example:

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
   int numero_randomico;
   srand((unsigned) time(NULL));
   numero_randomico = 1001+(rand())%4000;
   ...
}

6

To generate random integer values in C in the range [a, b):

a + rand() % (b - a)

For example, x % 5 can be one of the values 0, 1, 2, 3 or 4. So when do we rand() % (b - a) let’s generate a number between 0 and b - a - 1.

Representing by intervals:

[0, b - a - 1]

Now, if you add a both ends of the interval:

[a, b - 1]

What is the range you want (perhaps taking care to insert the 1 in the far right).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.