- Find 0~max;
- Find min~max;
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.
Previous article:Use the IO port of STC series MCU to directly drive the segment code LCD
Next article:Several common warnings in keil
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- EEWORLD University - Cadence Allegro 17.4 Quadcopter Full Zero-Based Introductory Course
- How to find MSP430 program examples on TI's official website
- Another board unboxing! This time it's the GigaDevice GD32307E-START development board
- FPGA Introduction Course 2-Counter
- Introduction to TI battery failure modes and BMS related solutions
- 2008 transcripts - review
- Help with the feedback control loop of the flyback switching power supply
- LPWAN Alternatives for IoT
- Greenhouse automatic spraying system ---- H743temperature sensor
- Hongmeng Development Board Neptune (Part 1) - Unboxing