How to generate a random int in C?

2018-12-31 05:23发布

Is there a function to generate a random int number in C? Or will I have to use a third party library?

标签: c random
24条回答
像晚风撩人
2楼-- · 2018-12-31 05:43

Hearing a good explanation of why using rand() to produce uniformly distributed random numbers in a given range is a bad idea, I decided to take a look at how skewed the output actually is. My test case was fair dice throwing. Here's the C code:

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

int main(int argc, char *argv[])
{
    int i;
    int dice[6];

    for (i = 0; i < 6; i++) 
      dice[i] = 0;
    srand(time(NULL));

    const int TOTAL = 10000000;
    for (i = 0; i < TOTAL; i++)
      dice[(rand() % 6)] += 1;

    double pers = 0.0, tpers = 0.0;
    for (i = 0; i < 6; i++) {
      pers = (dice[i] * 100.0) / TOTAL;
      printf("\t%1d  %5.2f%%\n", dice[i], pers);
      tpers += pers;
    }
    printf("\ttotal:  %6.2f%%\n", tpers);
}

and here's its output:

 $ gcc -o t3 t3.c
 $ ./t3 
        1666598  16.67%     
        1668630  16.69%
        1667682  16.68%
        1666049  16.66%
        1665948  16.66%
        1665093  16.65%
        total:  100.00%
 $ ./t3     
        1667634  16.68%
        1665914  16.66%
        1665542  16.66%
        1667828  16.68%
        1663649  16.64%
        1669433  16.69%
        total:  100.00%

I don't know how uniform you need your random numbers to be, but the above appears uniform enough for most needs.

Edit: it would be a good idea to initialize the PRNG with something better than time(NULL).

查看更多
临风纵饮
3楼-- · 2018-12-31 05:45

STL doesn't exist for C. You have to call rand, or better yet, random. These are declared in the standard library header stdlib.h. rand is POSIX, random is a BSD spec function.

The difference between rand and random is that random returns a much more usable 32-bit random number, and rand typically returns a 16-bit number. The BSD manpages show that the lower bits of rand are cyclic and predictable, so rand is potentially useless for small numbers.

查看更多
零度萤火
4楼-- · 2018-12-31 05:46

You want to use rand(). Note (VERY IMPORTANT): make sure to set the seed for the rand function. If you do not, your random numbers are not truly random. This is very, very, very important. Thankfully, you can usually use some combination of the system ticks timer and the date to get a good seed.

查看更多
还给你的自由
5楼-- · 2018-12-31 05:46

C Program to generate random number between 9 and 50

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

int main()
{
    srand(time(NULL));
    int lowerLimit = 10, upperLimit = 50;
    int r =  lowerLimit + rand() % (upperLimit - lowerLimit);
    printf("%d", r);
}

In general we can generate a random number between lowerLimit and upperLimit-1

i.e lowerLimit is inclusive or say r ∈ [ lowerLimit, upperLimit )

查看更多
皆成旧梦
6楼-- · 2018-12-31 05:46

rand() is the most convenient way to generate random numbers.

You may also catch random number from any online service like random.org.

查看更多
旧人旧事旧时光
7楼-- · 2018-12-31 05:47

On modern x86_64 CPUs you can use the hardware random number generator via _rdrand64_step()

Example code:

#include <immintrin.h>

uint64_t randVal;
if(!_rdrand64_step(&randVal)) {
  // Report an error here: random number generation has failed!
}
// If no error occured, randVal contains a random 64-bit number
查看更多
登录 后发表回答