A python random number function that includes 1?

I am new to Python and trying to create a program for a project. First, I need to create a dot between the numbers 0-1.0, including 0 and 1.0 ([0, 1.0]). I was looking for the python library for functions ( https://docs.python.org/2/library/random.html ) and I found this function:

random.random()

This will return the next random floating point number in the range [0.0, 1.0]. This is a problem because it does not include 1. Although the chances of creating 1 are slim in any case, it is still important because it is a scientific program that will be used in a larger data set.

I also found this function:

rand.randint

This will return an integer, which is also a problem.

I researched on the site and previously asked questions and found that this function:

random.uniform(a, b)

, , b.

- , python, [0, 1.0]?

, , . .

* .

+4
5

, random.uniform. , , , candidate > b 0, .

import sys
import random


def myRandom(a, b):
    candidate = uniform.random(a, b + sys.float_info.epsilon)
    while candidate > b:
       candidate = uniform.random(a, b + sys.float_info.epsilon)
    return candidate

gnibbler, . , , b > 0.

 candidate = uniform.random(a, b*1.000001)
+4

- ?

random.randint(0, 1000) / 1000.0

:

precision = 3
randomNumber = random.randint(0, 10 ** precision) / float(10 ** precision)
+3

:

import random
random.uniform(0.0, 1.0)

, [Python 3.x]:

N , a <= N <= b a <= b b <= N <= a b < a.

, , b , . ( ):

b a + (b-a) * random().

+1

:

list_rnd=[random.random() for i in range(_number_of_numbers_you_want)]
list_rnd=[item/max(list_rnd) for item in list_rnd]

. .

0

numpy floats, 64- 32- . b random.uniform(a, b), 1 :

import numpy
import random

def randomDoublePrecision():
    floatinfo = numpy.finfo(float)
    epsilon = floatinfo.eps
    a = random.uniform(0, 1 + eps)
    return a

, . .

0

Source: https://habr.com/ru/post/1537165/


All Articles