Home > Backend Development > Python Tutorial > How to import random in python

How to import random in python

(*-*)浩
Release: 2019-07-19 16:02:03
Original
11162 people have browsed it

The use of import in Python language is very simple, just use the import module_name statement to import. Here I mainly write about the essence of "import".

The import declaration combines two operations; it searches the named module and then binds the search results to the name in the local scope.

How to import random in python

random() method returns a randomly generated real number, which is in the range [0,1). (Recommended learning: Python video tutorial)

The following is the syntax of the random() method:

import random

random.random()
Copy after login

Note: random() cannot be directly To access, you need to import the random module and then call the method through the random static object.

Example

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import random
 
# 生成第一个随机数
print "random() : ", random.random(1,10)
 
# 生成第二个随机数
print "random() : ", random.random(1,10)
Copy after login

The output result after running the above example is:

2
5
Copy after login

More Python related technical articles , please visit the Python Tutorial column to learn!

The above is the detailed content of How to import random in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template