I need to generate random numbers in C [duplicate]

2020-03-24 04:44发布

问题:

This question already has answers here:
Closed 10 years ago.

Possible Duplicates:
How to generate a random number in C?
implementation of rand()
Generating random terrain in Blender3D

I need high quality random numbers in C, but I have no idea what to really do. I need to be able to get numbers from 1-100. Any help or maybe point me to where I can find help.

回答1:

This is the simplest method of producing uniformly distributed random numbers in C:

Step 1. Be sure to include the standard library header to get the necessary function prototypes

#include <stdlib.h>

Step 2. Seed the random number generator using srand(). The seed determines where the random numbers start. The sequence of random numbers will always be exactly the same for a given seed. This allows you to have random, yet reproducible results. If you don't need it to be reproducible, a good thing to seed with is the current time, so that the random sequence will be different on each run.

srand(time(NULL));

(be sure to include time.h if you do this). Also, only seed the generator once per program run unless you are generating a huge number (millions or billions) of random numbers. Seeding frequently makes the sequence less random.

Step 3. Get your random number.

rand()

This function returns a random number between 0 and RAND_MAX, which is a macro that is defined as a rather large integer.

Step 4. Get your random number into the range you want. The general formula for doing so is this:

int random_number = rand() % range + min;

Where range is how many (consecutive) numbers you want to choose from, and min is the smallest of these. So to generate a number between 1 and 100, range is 100 and min is 1:

int random_number = rand() % 100 + 1;

Some people object to this formula because it uses the low-order bits of the number given by rand(), and in older implementations of software pseudo-random number generators these were often less random than the high order bits, but on any modern system this method should be perfectly fine.



回答2:

By "fair distribution", I assume you mean that you're not generally satisfied by rand(). In this case, you should probably use OS-specific methods that produce cryptographically secure random numbers - /dev/random or /dev/urandom (depending on your needs) on Unix, and CryptGenRandom or RtlGetRandom on Win32. On Win32 specifically, if you're using VS2005 or later, you may just use rand_s.



回答3:

http://mathworld.wolfram.com/RandomNumber.html

This article explains the basics to creating your own random number generator that will outperform the standard C library function if you find it lacking in distribution. It produces a better spread and hence a more random number.

This is critical if you hope to produce something that can not be reverse engineered, like for poker sites.



回答4:

You can try something like this:

main()
{
  srand(time(0));

  for(int i=0;i<1000;++i)
    printf("%f ", ((float)rand())/RAND_MAX*99+1);

  return 0;
}

It's as uniform a distribution as the standard rand() can give.



回答5:

The standard C library has rand which will probably be sufficient, unless you have a need for a prng with a particular statistical distribution..



回答6:

As an high-quality random number generator, please do not use rand(), or not-Quality-Assured code. It is extremely easy to generate random number incorrectly (check Knuth's funny story in "The art of Computer Programming: Seminumerical Algorithms"). The easiest way to proceed is maybe to use the generators in GNU's scientific library. It installs with one click on *nux, and there are several ports of the GSL to Windows. I've used it. It is easy to call and works well.



回答7:

The other posts have good advice. If you really want to dive into the guts of random number generation, take a look at Numerical Recipes in C. Start with Chapter 7.



回答8:

If you really need lottery-quality random numbers, I don't think you want a digital algorithm at all.

You want an actual physical process. See Random.org. Are you willing to pay?

Beware programmatic random number generators if you really need random numbers. As we've seen in the answers so far, it's not only hard to write a good random(), it's hard to figure out how to use the output from it correctly, whether by modulo or by scaling. Even code that people see as "obvious" often turns out to be subtly incorrect.

Taking the ints into floats or doubles only brings in all the quirks of floating point numbers (such as their ability to represent more numbers close to zero than close to one).


What are your requirements? Will your numbers need to be certified? Or do you just want really good random numbers? What tests will you employ to see if your random generator is "good?"