In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "C language random number rand () function how to use", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "C language random number rand () function how to use" this article.
Random numbers need to be generated in many scenes in life, such as raffles, cards, games and so on. In the C language standard library function, there is a function rand specially used to generate random numbers, and its function prototype is as follows:
Int _ cdecl rand (void)
The rand function has no arguments, and its return value is a random number. Let's test the rand function with a simple example.
# include # include int main (int argc, char** argv) {int i; i = rand (); printf ("% d", I); return 0;}
Call the rand function directly to generate a random number and print it out. The running results are as follows:
At this time, the random number generated is 41. Let's produce a few more random numbers.
Ten random numbers are generated through a for loop, and the printed results show that the first random number is 41, and the other nine data are relatively large. Change the random number to 15 and test it again.
At this time, 15 random numbers are generated, but compared with generating 10 random numbers, it can be found that the first 10 random numbers generated this time are exactly the same as last time. After rerunning the program, the random number generated is exactly the same as the last time.
What is the reason for this? Is there a bug in the random number generating function rand?
In fact, the random number generated by the rand function is a pseudo-random number, which is based on a numerical value and then calculates a value through the formula, and returns the calculated result as a random number. This fixed reference value is called a "seed". Each time the computer starts, a seed is generated, and then the seed will not change during the later run. So every time the rnad function is executed, its reference value seed value is fixed, so the random number generated each time is fixed. If you want to generate a different random number each time, you need to change the value of the seed before each random number is generated.
The seed value is generated by the srand function.
The function prototype is as follows:
Void _ _ cdecl srand (unsigned int _ Seed)
The function does not return a value, and its parameters are the seed values to be set. The function that modifies the seed is also added to the code.
Set the seed value to 10 and generate 10 random numbers.
Set the seed value to 20 and generate 10 random numbers.
You can see that when the value of the seed changes, so does the random number generated. If random is used repeatedly in the program, then the value of the random seed cannot be a fixed value, it must change all the time, so that the generated random number can change all the time. The number that changes all the time in the system, the first thing you can think of is time, because time is changing in real time, and it is impossible to have the same value.
Then you can get the time of the system and then use it as the seed of a random number. The prototype of the time function time is as follows:
_ _ CRT_INLINE time_t _ cdecl time (time_t * _ Time) {return _ time64 (_ Time);}
When the time function is called, it returns the number of seconds of the current system's time, and its parameter can be directly set to NULL.
When you call the time function, you need to add the header file time.h. Use the time value as a random seed, so that the random number generated each time will be different, but as you can see from the output above, the size of each generated random number varies greatly. What if you want to control the size of the random number within a certain range?
To control the range of numbers, you can use the mathematical remainder operation. If you want to control the generated number to less than 10, divide the generated number by 10, and then take the remainder. So the size of the remainder will not exceed 10.
Here, divide the generated random number by 100 to take the remainder, so that the final random number will be less than 100.
The above is all the contents of the article "how to use the random number rand () function in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.