In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Python generated random number case analysis", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Python generated random number case analysis"!
Random number seed
Why do we propose random number seeds? As we mentioned earlier, random numbers are simulated, and if you want to simulate them more truly, you need to change the values in the seed function, generally using the timestamp as the seed of the random function. For example, in the following case, when the random number seed is fixed, the random number generated will also be fixed. By default, the system uses the timestamp as the seed to generate random numbers. Single timestamp
Random timestamp
The first result
The second result
Second, generate random numbers
The following lifetime into 10 random numbers from 1 to 100 as an example
1.random ()
The random number that generates [0-1) is float. Most of the following functions are generated by random numbers based on this function, and random numbers that want to generate response areas can be multiplied by a corresponding integer using this function.
From random import * for i in range (10): print (int (random () * 100mm 1), end= ") print () 2.ranint (amemb)
Randomly generate an integer of aMub
From random import * for i in range (10): print (randint (1100), end= "") 3.randrange (start,stop [, step])
There are three elements: start, end and step size, including the lower limit but not the upper limit when generating random numbers.
From random import * for i in range (10): print (int (randrange (1101)), end= ") 4.getrandbits (k)
Returns a random integer with a bit length of k bits.
From random import * for i in range (10): print (int (getrandbits (4)), end= "") 3. Generate random sequence 1.choice (seq)
Randomly select one from a given sequence
The code is as follows:
From random import * test=: print (choice (test)) 2.samplex (sequence, k)
K elements are randomly selected from the sequence, which will not be repeated. (need to satisfy len (sequence) > = k)
The code is as follows:
From random import * test= [1, 23, 3, 22, 13] print (sample (test,3)) 3.shuffle (x [, random])
The purpose of this function is to sort randomly, based on the original sequence.
The code is as follows:
From random import * test=] shuffle (test) print (test) Thank you for your reading. The above is the content of "Python generating random number instance analysis". After the study of this article, I believe you have a deeper understanding of the problem of Python generating random number instance analysis, and the specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.