Is there a function to generate a random int number in C? Or will I have to use a third party library?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
If your system supports the
arc4random
family of functions I would recommend using those instead the standardrand
function.The
arc4random
family includes:arc4random
returns a random 32-bit unsigned integer.arc4random_buf
puts random content in it's parameterbuf : void *
. The amount of content is determined by thebytes : size_t
parameter.arc4random_uniform
returns a random 32-bit unsigned integer which follows the rule:0 <= arc4random_uniform(limit) < limit
, where limit is also an unsigned 32-bit integer.arc4random_stir
reads data from/dev/urandom
and passes the data toarc4random_addrandom
to additionally randomize it's internal random number pool.arc4random_addrandom
is used byarc4random_stir
to populate it's internal random number pool according to the data passed to it.If you do not have these functions, but you are on Unix, then you can use this code:
The
urandom_init
function opens the/dev/urandom
device, and puts the file descriptor inurandom_fd
.The
urandom
function is basically the same as a call torand
, except more secure, and it returns along
(easily changeable).However,
/dev/urandom
can be a little slow, so it is recommended that you use it as a seed for a different random number generator.If your system does not have a
/dev/urandom
, but does have a/dev/random
or similar file, then you can simply change the path passed toopen
inurandom_init
. The calls and APIs used inurandom_init
andurandom
are (I believe) POSIX-compliant, and as such, should work on most, if not all POSIX compliant systems.Notes: A read from
/dev/urandom
will NOT block if there is insufficient entropy available, so values generated under such circumstances may be cryptographically insecure. If you are worried about that, then use/dev/random
, which will always block if there is insufficient entropy.If you are on another system(i.e. Windows), then use
rand
or some internal Windows specific platform-dependent non-portable API.Wrapper function for
urandom
,rand
, orarc4random
calls:The
rand()
function in<stdlib.h>
returns a pseudo-random integer between 0 andRAND_MAX
. You can usesrand(unsigned int seed)
to set a seed.It's common practice to use the
%
operator in conjunction withrand()
to get a different range (though bear in mind that this throws off the uniformity somewhat). For example:If you really care about uniformity you can do something like this:
If you need better quality pseudo random numbers than what
stdlib
provides, check out Mersenne Twister. It's faster, too. Sample implementations are plentiful, for example here.Well, STL is C++, not C, so I don't know what you want. If you want C, however, there is the
rand()
andsrand()
functions:These are both part of ANSI C. There is also the
random()
function:But as far as I can tell,
random()
is not standard ANSI C. A third-party library may not be a bad idea, but it all depends on how random of a number you really need to generate.Lets go through this. First we use the srand() function to seed the randomizer. Basically, the computer can generate random numbers based on the number that is fed to srand(). If you gave the same seed value, then the same random numbers would be generated every time.
Therefore, we have to seed the randomizer with a value that is always changing. We do this by feeding it the value of the current time with the time() function.
Now, when we call rand(), a new random number will be produced every time.
FWIW, the answer is that yes, there is a
stdlib.h
function calledrand
; this function is tuned primarily for speed and distribution, not for unpredictability. Almost all built-in random functions for various languages and frameworks use this function by default. There are also "cryptographic" random number generators that are much less predictable, but run much slower. These should be used in any sort of security-related application.