C-language random number algorithm code under Linux

Source: Internet
Author: User
Tags file system printf rand random seed uuid linux

Take random numbers under Linux, of course, you can simply use the RAND function, but note that you must set a good seed, otherwise pseudorandom numbers will become very pseudo random number. To set the seed, the time function is usually used to return the current date. Generally speaking, this approach is basically OK, because although we are using random numbers, but because the seeds are different, it is almost impossible to guess our seeds from hundreds of billions of numbers.

But if our seed algorithms are known, then obviously we can't. Of course it doesn't matter as some apps, like we're going to randomly draw a little piggy on the screen. It's almost impossible for anyone to care about the outcome of the next one. But some applications are not the same, big do not say, is some games, also have to consider the safety of random numbers.

The simple approach is that our seeds are also expressed in random numbers. But then there seems to be a chicken or egg problem. Fortunately, Linux provides us with "real" random numbers, in which Linux maintains occasional data and provides access to the user. is called a real random number, this is because the data comes from accidental operations on the computer itself, such as hard disk operations, keyboard and mouse operations, and so on, which are certainly more realistic than the pseudo-random numbers generated by the fixed algorithm, where we have a cool name called "Entropy". The interface provided by the kernel is/dev/random and/dev/urandom devices, the difference being that the random will definitely return a number when read, and block if there is not enough data. Urandom does not block, but it does not guarantee that the appropriate data will be returned.

In the following code, the function init_random is used to generate a random seed, and then direct rand to get the random number. The reason why you read it 512 times and then all together is that the URANDOM device does not guarantee that every time you read it is real data.

Another function, My_rand, is to get the real random number by simply reading the random device. So every time is a real random number, but the problem is if the system's "entropy" is not enough, then the program will block. For applications with high security requirements, you can use this approach. If the "entropy" is not enough, it can artificially "create" some entropy. For example, the following program, if you do not do any operation, perhaps output a few random numbers after the program will stop the output, this is the other terminal to run some of the more busy process, such as "Find/", it will be found that our program to start a steady stream of output random number.

There are many examples of using random devices, such as GPG will let you constantly knock the keyboard when it comes to creating keys until it is satisfied. For the kernel after 2.4, you can also get more information about the random device through the proc file system interface, such as/proc/sys/kernel/random/entropy_avail can know how much "entropy" is available in the system. Running our example can be found, this value is reduced to single digits, until the program blocked. Another file is also interesting, that is/proc/sys/kernel/random/uuid, through this interface can easily get the truly unique UUID.

There is something to say about the random device, which is system security. Some people can decipher some information through the process of system startup or the content of entropy produced by the system during a certain period of time, which sounds very advanced, but in theory it is entirely possible. Because the random device is based on the system noise to get the entropy, but if two times the system start completely consistent, then the START process generated entropy will of course be exactly the same. However, these security vulnerabilities do not concern us, because the system now has a relevant patch.

#include <stdio.h>
#include <sys/time.h>
#include <fcntl.h>

void Init_random ()
{
unsigned int ticks;
struct Timeval TV;
int FD;
Gettimeofday (&AMP;TV, NULL);
Ticks = tv.tv_sec + tv.tv_usec;
FD = open ("/dev/urandom", o_rdonly);
if (FD > 0)
{
unsigned int r;
int i;
for (i = 0; i < i++)
{
Read (FD, &r, sizeof (R));
Ticks + = r;
}
Close (FD);
}

Srand (ticks);
printf ("Init finished");
}

unsigned int new_rand ()
{
int FD;
unsigned int n = 0;
FD = open ("/dev/urandom", o_rdonly);
if (FD > 0)
{
Read (FD, &n, sizeof (n));
}
Close (FD);
return n;
}

int main ()
{
int n, I;
Init_random ();
n = rand ();
printf ("n=%d", N);
for (i=0;i<10;i++)
printf ("%u", New_rand ());
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.