Python random number details and instance code, python Random Number
Python3 Random Number
- Random is used to generate random numbers. We can use it to randomly generate numbers or select strings.
- Random. seed (x) changes the seed of the random number generator.
- Generally, you do not need to set seed. Python automatically selects seed.
- Random. random () is used to generate a random floating point number n, 0 <= n <1
- Random. uniform (a, B) is used to generate a random floating point in a specified range. The generated random integer a <= n <= B;
- Random. randint (a, B) is used to generate an integer in the specified range. a is the lower limit, B is the upper limit, and the generated random integer a <= n <= B; if a = B, n = a. If a> B, an error is returned.
- Random. randrange ([start], stop [, step]) obtains a random number from the set in the specified range [start, stop) That increments by the specified base number. The default base value is 1.
- Random. choice (sequence) gets a random element from the sequence. The sequence parameter indicates an ordered type, which is not a specific type. It generally refers to list, tuple, string, and so on.
- Random. shuffle (x [, random]) is used to disrupt (shuffling) elements in a list and change the original list.
- Random. sample (sequence, k) randomly obtains k elements from the specified sequence and returns them as a clip without changing the original sequence.
However, you must note that Python random is a pseudo-random number.
Then, can I use python random to implement a real random number? The answer is No. The so-called true random number is the number of random events generated according to the absolute random event. That is to say, a random event without a causal relationship is required. Therefore, this thing only exists in the field of philosophy ......
Currently, random numbers are generated at random in Statistics. Because the random source is a natural event, it is regarded as a chaotic variable at the top of the Day, and absolute no cause or effect probably does not exist.
Linear regression is enough for statistics on random ......
Zookeeper is still honestly using the random module ....
Code demo
Import random # random integer import stringprint (random. randint (100) # random print (random. randrange (0,101, 2) # random floating point print (random. random () print (random. uniform (1, 10) # random character print (random. choice ('abcdefg & # % ^ * F') # select a specific number of characters in print (random. sample ('abcdefghj', 3) # select a specific number of characters from multiple characters to form a new string # print (string. join (random. sample (['A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'I ', 'J'], 3 )). replace ("", "") # randomly select the string print (random. choice (['apple', 'pear ', 'peach', 'Orange ', 'lemon']) # shuffling items = [1, 2, 3, 4, 5, 6] random. shuffle (items) print ("shuffling:", items) # obtain k elements from the specified sequence at random and return them as a single segment without changing the original sequence list = [] list = random. sample (items, 2) print (list)
Merge merge results
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!