MCU random numbers: rand(), srand()

Publisher:神光骑士Latest update time:2016-09-09 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
From the book, I saw that the rand() function returns a pseudo-random number between 0 and 32767. srand sets the initial value of the pseudo-random number.
In normal use, most usage scenarios are:
  1. Find 0~max;
  2. Find min~max;
I wrote a few functions as extensions. C does not support variable parameters or overload, so we can only use different function names. Of course, we can also pass in a hash data structure as a parameter (ruby-like).

int rand_with_min_max(int min,int max)
{
return ((rand()/max) + min);
}
int rand_with_min(int min)
{
srand(min);
return rand();
}
int rand_with_max(int max)
{
return (rand()/max);
}

 

srand() provides seed to rand()

If the value entered into srand is the same each time, then the random numbers generated each time will also be the same.

srand(n)

for(10)

rand()
, that is to say, using a fixed value as the seed is a disadvantage. The usual practice is to replace it with a line of code like srand((unsigned) time(NULL));, which will make the seed an unfixed number, so that the generated random number will not be the same every time it is executed.


1,先看一个例子 
#include  
#include  
#include  
using namespace std; 
int main( void ) 

int i; 
/* Seed the random-number generator with current time so that 
* the numbers will be different every time we run. 
*/ 
srand( (unsigned)time( NULL ) ); 
/* Display 10 numbers. */ 
for( i = 0; i < 10;i++ ) 
printf( " %6d/n", rand() ); 
}

2. About time.h 
time.h contains many interesting functions, such as 
char *ctime(long *clock). 
This function converts the time indicated by clock (such as the time returned by the time function) into 
a string in the following format: Mon Nov 21 11:31:54 1983/n/0

#i nclude  
#i nclude  
#i nclude  
using namespace std;


void main() 

time_t t1,t2; 
char getTime[20]; 
char *ptstring=getTime;

int x,count=0; 
x=RAND_MAX; 
cout<<<'/n'; 
t1=time(NULL); 
ptstring=ctime(&t1);

while(count<=100) 

srand( (unsigned)time( NULL ) ); 
x=rand()%50; 
if(x<5) 
continue; 
else 

count++; 
cout<<"the numth is "<<<'/n'; 

}

Checking the value of ptstring will display "Tue Sep 13 16:31:06 2005"

3. Finally, let’s talk about the srand() function 
void srand(unsigned seed) to initialize the random number generator

The discussion is as follows:  1. I think there are at least three reasons 
why the C library does not put the important operation of using the system clock to initialize the random seed directly into the implementation of the rand function: (1) It can efficiently generate continuous random numbers without initializing each time;  (2) It gives programmers greater flexibility, because in situations with higher requirements, better data should be used as seeds instead of the system clock;  (3) For those who just want to generate a large number of pseudo-random numbers for some kind of verification or statistics, initialization is not necessarily necessary. At most, the program will generate the same series of random numbers every time it runs - in some cases, this does not matter.








In fact, there is a more important reason: 
as a pseudo-random sequence generator, the rand() function must have an important feature: the generated sequence

must be reproducible. 
This is not just an algorithm, but to a large extent, it is related to the accuracy of code testing. If the algorithm

uses data related to the result of rand(), through a controllable and reproducible sequence, we have the opportunity to reproduce

the process of each test, so as to find the problem more effectively. 
So here is a suggestion: in the code, if the function result of rand() is related to the result of the algorithm, then

you must ensure that your rand() call is reproducible.

4. Usage of functions rand() and srand() in C language - -

rand(void) is used to generate a pseudo-random unsigned int integer. 
srand(seed) is used to set the seed for the rand() function.

srand and rand should be used together. Generally speaking, srand is used to set rand. 
For example: 
srand((UINT)GetCurrentTime()); 
int x = rand() % 100; 
generates a random number between 0 and 100.

srand() is used to initialize the random seed number. Since the internal implementation of rand is done with the linear congruential method, it is not a true

random number. It is just because its period is very long, so it can be considered random within a certain range. The formula is as follows


rand = rand*const_1 + c_var; 
The srand function is given the first rand value.

Using "int x = rand() % 100;" to generate a random number between 0 and 100 is not a good method. A  better approach is: j=(int)(n*rand()/(RAND_MAX+1.0)) to generate a random number
between 0 and n. RAND_MAX=0x7fffffff


5. Summary 
1) srand() provides seeds for rand() 
2) The seed in srand() is usually obtained from the time function, e.g. srand((UINT)GetCurrentTime()) srand( (u

nsigned)time( NULL ) ) time() function gets the current system time...etc.


Reference address:MCU random numbers: rand(), srand()

Previous article:Use the IO port of STC series MCU to directly drive the segment code LCD
Next article:Several common warnings in keil

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号