Tips to distinguish randrange() and uniform() in python

Y2J
Release: 2017-05-17 13:19:09
Original
2731 people have browsed it

We can know from the function signature:

In [7]: random.randrange?
Signature: random.randrange(start, stop=None, step=1, _int=<type &#39;int&#39;>, _maxwidth=9007199254740992L)
Docstring:
Choose a random item from range(start, stop[, step]).

This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
File:      /usr/lib/python2.7/random.py
Type:      instancemethod

In [8]: random.uniform?
Signature: random.uniform(a, b)
Docstring: Get a random number in the range [a, b) or [a, b] depending on rounding.
File:      /usr/lib/python2.7/random.py
Type:      instancemethod
Copy after login

randrange is randomly selected from range(start, stop[, step]), and the generated one must be int;
uniform is from [a, b) or generate a random number in [a, b], the generated number is float;
these two usage scenarios are different.

[Related recommendations]

1.Special recommendation:"php programmer toolbox" V0.1 version Download

2. Free Python video tutorial

3. Detailed explanation of uniform() based on Python

The above is the detailed content of Tips to distinguish randrange() and uniform() in python. For more information, please follow other related articles on the PHP Chinese website!

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