Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of generating Random numbers by Python

Shulou Source: shulou.com Published: 2022-06-01 06:59:31 10月25日 Update

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!

Tags: Random numbers generation functions sequences seeds examples case analysis analysis integers time code learning elements content that is time results extraction sorting lifetime Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Xiaomi Microsoft Huawei