Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does python generate two random numbers and compare them?

Shulou Source: shulou.com Published: 2022-05-31 11:14:11 10月04日 Update

This article mainly introduces python how to generate two random numbers and compare the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this python how to generate two random numbers and compare the article will have a harvest, let's take a look at it.

Specific steps for use:

1. First open the python editor and create a new python project.

2. Import the random module using the import keyword in the python project.

Import random

3. Use the random () method to generate two random numbers.

Print random.random ()

Print random.random ()

4. Use seed to set the seed, and then use the random () method to generate two random numbers for comparison.

Random.seed (10)

Print "Random number with seed 10:", random.random ()

Random.seed (10)

Print "Random number with seed 10:", random.random ()

You will find that the generated random numbers are all the same.

The complete code is as follows:

#! / usr/bin/python

#-*-coding: UTF-8-*-

Import random

Print random.random ()

Print random.random ()

Print "- set seed seed -"

Random.seed (10)

Print "Random number with seed 10:", random.random ()

# generate the same random number

Random.seed (10)

Print "Random number with seed 10:", random.random ()

# generate the same random number

Random.seed (10)

Print "Random number with seed 10:", random.random ()

Execution result:

0.739880443211

0.78969784712

-set seed seed-

Random number with seed 10: 0.57140259469

Random number with seed 10: 0.57140259469

Random number with seed 10: 0.57140259469

This is the end of the article on "how to generate two random numbers and compare them with python". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to generate two random numbers and compare them with python". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Random numbers generation two knowledge seeds content methods articles projects code values keywords keywords easy to operate articles easy to understand more modules steps see Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia OPPO Reno Xiaomi Shulou Information