In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to generate pseudo-random numbers with rand () and srand () functions in C language". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to use rand () and srand () functions to generate pseudo-random numbers in C language" can help you solve the problem.
Rand () returns a random value ranging from 0 to RAND_MAX. Before calling this function to generate a random number, you must first use srand () to set the random number seed. If no random number seed is set, rand () will automatically set the random number seed to 1 when it is called. Srand () is used to set the random number seed when rand () generates a random number. The parameter seed must be an integer, and you can usually use the return value of geypid () or time (0) as seed. If seed is set to the same value each time, the random value generated by rand () will be the same each time.
The standard library (included in) provides two functions that help generate pseudo-random numbers:
Function 1: int rand (void)
Returns a random integer between [seed, RAND_MAX (0x7fff)), starting with the seed specified in srand (seed).
Function 2: void srand (unsigned seed)
The parameter seed is the seed of rand () and is used to initialize the starting value of rand ().
You can assume that every time rand () is called, it looks at:
1) if the user has previously called srand (seed) and assigned a value to seed, it will automatically call srand (seed) once to initialize its starting value.
2) if the user has not called srand (seed) before, it will automatically call srand (1) once.
According to the points above, we can draw the following conclusion:
1) if you want rand () to produce a different value each time the program runs, you must give a variable value to seed in srand (seed), which must be different each time the program runs (such as the time elapsed so far).
2) otherwise, if seed is assigned a fixed value, the value produced by rand () will be the same each time the program runs, although this value will be a random value between [seed, RAND_MAX (0x7fff)).
3) if srand (seed) is not called before calling rand (), the effect will be the same as calling srand (1) and then calling rand () (1 is also a fixed value).
To give a few examples, suppose we want to get a random integer between 0,6 (excluding 6 itself):
For example 1, no seed is specified:
For (int iTuno Bandi)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.