1. Basic functions
The functions required to take random numbers in C language are:
int rand (void);
void Srand (unsigned int n);
The rand () function and the Srand () function are declared in the header file Stdlib.h, so you must include the header file to use these two functions:
#include <stdlib.h>
2. How to use
The rand () function returns a pseudo-random number (pseudorandom) between 0 and Rand_max. The Rand_max constants are defined in the Stdlib.h header file. The value is equal to 32767, or greater.
The Srand () function uses the independent variable n as the seed to initialize the random number generator. As long as the same seed is passed into Srand (), and then Rand () is invoked, the same sequence of random numbers is produced. Therefore, we can avoid repetition by taking time as the seed of the Srand () function. If you do not invoke Srand () before calling Rand (), you will have the same result as if you had called Srand (1) beforehand.
* Example 1: Do not specify the value of the seed * *
for (int i=0; i<10; i++)
{
printf ("%d", rand ()%10);
}
Each run will output: 1 7 4 0 9 4 8 8 2 4
* Example 2: The value of the specified seed is 1 */
Srand (1);
for (int i=0; i<10; i++)
{
printf ("%d", rand ()%10);
}
Each run will output: 1 7 4 0 9 4 8 8 2 4
The output of Example 2 is exactly the same as for example 1.
* Example 4: Specify a seed value of the current time * *
Srand ((unsigned) time (NULL));
for (int i=0; i<10; i++)
{
printf ("%d", rand ()%10);
}
The program does not work the same every time it runs, because every time you start a program is different. Also note that you must include the header file time.h before you use the time () function.
3. Attention to a certain range of random number.
To take a random integer between [0,10], you need to modulo the return value of rand () with 10. Randnumber = rand ()% 10;
So, if the value does not start at 0. You just have to remember a generic formula.
To take a random integer (including a, but not including b) between [a,b), use:
(rand ()% (b-a)) + a pseudo-random floating-point number.
To get the floating-point number between 0~1, you can use:
RAND ()/(double) (Rand_max)
If you want to take a larger range of random floating-point numbers, such as 0~100, you can use the following methods:
RAND ()/(double) (Rand_max)/100)
Other circumstances, and so on, are not described here in detail.
Of course, the method of pseudo random floating-point number is only used to illustrate the use of functions, you can use a better way to achieve.
For example, suppose we want to get a random integer between 0~10 (excluding 10 itself):
We may have discussed many times how random numbers are generated in the computer, and in this article, I will discuss this issue in more depth and explain my understanding of the problem.
The first thing to declare is that the computer does not produce an absolutely random random number, and the computer can only produce "pseudorandom numbers". In fact, the absolute random random number is only an ideal random number, even if the computer development, it will not produce a series of absolute random random number. A computer can only generate a relative random number, or pseudo random number.
Pseudo-random number is not a false random number, here the "pseudo" is a regular meaning, is the computer generated pseudo-random number is both random and regular. How to understand it. The pseudorandom numbers produced sometimes obey certain laws and sometimes do not obey any laws; some of the pseudorandom numbers obey certain rules; the other part does not follow any laws. For example, "There are no two leaves in the same shape," which is the characteristics of things, that is, randomness, but the leaves of each tree have an approximate shape, which is the commonness of things, that is, regularity. From this point of view, you will probably accept the fact that computers can only produce pseudo random numbers and cannot produce random numbers that are absolutely random.
So how does a random number in a computer produce it? One might say that random numbers are generated by "random seeds." Yes, random seeds are used to produce a number of random numbers, and in a computer, such a "random Seed" is a number of unsigned reshaping. So where does the random seed come from?
Here's a C program that looks like this:
rand01.c
#include <dos.h>
static unsigned int rand_seed;
unsigned int random (void)
{
Rand_seed= (rand_seed*123+59)%65536;
return (rand_seed);
}
void Random_start (void)
{
int temp[2];
Movedata (0x0040,0x006c,fp_seg (temp), Fp_off (temp), 4);
RAND_SEED=TEMP[0];
}
Main ()
{
unsigned int i,n;
Random_start ();
for (i=0;i<10;i++)
printf ("%u\t", Random ());
printf ("\ n");
}
This program (RAND01.C) fully describes the process of generating random numbers:
First, the main program calls the Random_start () method, the sentence in the Random_start () method I am interested in:
Movedata (0x0040,0x006c,fp_seg (temp), Fp_off (temp), 4);
This function is used to move the memory data, where FP_SEG (far pointer to segment) is the function that takes the address of the temp array segment, and Fp_off (far to offset) is the function that takes the relative address of the temp array, The role of the Movedata function is to place the two words in the 0040:006CH storage unit in the two storage cells in the declaration of the array temp. This allows a 16-bit number at the 0040:006ch to be sent to rand_seed through the temp array.
Random is used to calculate the random number based on the value of the random seed rand_seed, the sentence:
Rand_seed = (rand_seed*123+59)%65536;
is a method for calculating random numbers, and the method of calculating random numbers is different in different computers, even in different operating systems installed on the same computer. I've tried it on Linux and Windows, and the same random seed is different in terms of the random numbers generated in both operating systems, which means they're calculated differently.
Now, we know where random seeds are obtained, and how random numbers are calculated from random seeds. Why, then, should random seeds be taken at the 0040:006ch of the memory. What is stored in the 0040:006ch place.
The person who has studied the course "Computer composition principle and interface technology" may recall that the Intel 8253 Timing/counter was used when compiling the ROM BIOS Clock Interrupt service program, and its communication with Intel 8259 interrupt chip enabled the interrupt service program to operate. The 18.2 interrupts that the motherboard produces per second are the result of the processor controlling the interrupt chip based on the timer/register value. On our computer's motherboard there will be a timer/register to calculate the current system time, each clock signal cycle will make the register plus one, and the value of this register is stored where. Yes, in the memory of the 0040:006ch, in fact this section of memory space is so defined:
Timer_low DW? ; address is 0040:006ch
Timer_high DW? ; address is 0040:006eh
Timer_oft DB? ; address is 0040:0070h
In the clock interrupt service program, whenever the Timer_low is full, at this time, the register will also be full, the value of the register to zero, that is, timer_low at the 16-bit binary zero, and Timer_high plus one. In the rand01.c
Movedata (0x0040,0x006c,fp_seg (temp), Fp_off (temp), 4);
It is the Timer_low and Timer_high two 16-bit binary numbers into the temp array, and then sent to the rand_seed, thus obtaining a "random seed."
Now, it is certain that the random seed comes from the system clock, specifically from the memory value of the timer/counter on the computer's motherboard. In this way, we summarize the previous analysis and discuss the application of these conclusions in the program:
1. Random numbers are calculated by a random seed based on a certain calculation method. So, as long as the calculation method is certain, random seed is certain, then the random number that produces will not change.
Look at the following C + + program://rand02.cpp
#include <iostream>
#include <ctime>
using namespace Std;
int main ()
{
unsigned int seed = 5;
Srand (seed);
unsigned int r = rand ();
cout << R << Endl;
}
In the same platform environment, after compiling the build exe, each time it runs, the random number displayed is the same. This is because in the same compilation platform environment, random seed generation random number of calculation methods are the same, coupled with random seeds, so that the resulting random number is the same.
2. As long as the user or third party does not set the random seed, then by default the random seed comes from the system clock (that is, the Timer/counter value)
Look at the following C + + program:
Rand03.cpp
#include <iostream>
#include <ctime>
using namespace Std;
int main ()
{
Srand ((unsigned) time (NULL));
unsigned int r = rand ();
cout << R << Endl;
return 0;
}
Here the user and other programs do not set random seeds, the value of the system Timer/counter is used as a random seed, so, in the same platform environment, after compiling the build exe, each time it is run, the random number displayed will be pseudo random number, that is, the results of each run display will be different.
3. Recommendation: If you want to generate a sequence of random numbers in a program, you need to set up a random seed at most before generating a random number.
Look at the following C + + program used to generate a random string:
Rand04.cpp
#include <iostream>
#include <time.h>
using namespace Std;
int main ()
{
int Rnum,m