243

I want a random number between 0 and 1, like 0.3452. I used random.randrange(0, 1) but it is always 0 for me. What should I do?

martineau
  • 119,623
  • 25
  • 170
  • 301
Talia
  • 2,947
  • 4
  • 17
  • 28
  • 4
    @gidim: this is more specific question e.g., `random.random()` (`[0,1)`) is the answer to this question but not the question you've linked and therefore it is not a duplicate. – jfs Oct 27 '15 at 04:28
  • 1
    randrange(a,b) returns INTEGERS between a (incl.) and b (excl.) – Julien Oct 27 '15 at 06:26
  • import numpy as np np.random.random_sample((120)) – BigData-Guru Aug 23 '20 at 19:20

7 Answers7

357

You can use random.uniform

import random
random.uniform(0, 1)
jh314
  • 27,144
  • 16
  • 62
  • 82
119

random.random() does exactly that

>>> import random
>>> for i in range(10):
...     print(random.random())
... 
0.908047338626
0.0199900075962
0.904058545833
0.321508119045
0.657086320195
0.714084413092
0.315924955063
0.696965958019
0.93824013683
0.484207425759

If you want really random numbers, and to cover the range [0, 1]:

>>> import os
>>> int.from_bytes(os.urandom(8), byteorder="big") / ((1 << 64) - 1)
0.7409674234050893
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 7
    random.random return values between [0,1) so then return would never be 1. – gidim Oct 27 '15 at 04:03
  • 1
    @gidim: `randrange(a, b)` excludes `b` as well as anything `*range()` in Python. Python uses [option a)](https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html). OP shows `randrange()`, not `randint()` that includes both edges. – jfs Oct 27 '15 at 04:29
  • 2
    Even if it would include, the probability to return 1 would be extremely low (as in practice there is a 64 digits precision of python's float), so it wouldn't change much. – szedjani Sep 30 '19 at 09:34
38

I want a random number between 0 and 1, like 0.3452

random.random() is what you are looking for:

From python docs: random.random() Return the next random floating point number in the range [0.0, 1.0).


And, btw, Why your try didn't work?:

Your try was: random.randrange(0, 1)

From python docs: random.randrange() Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.

So, what you are doing here, with random.randrange(a,b) is choosing a random element from range(a,b); in your case, from range(0,1), but, guess what!: the only element in range(0,1), is 0, so, the only element you can choose from range(0,1), is 0; that's why you were always getting 0 back.

alsjflalasjf.dev
  • 1,589
  • 13
  • 11
19

you can use use numpy.random module, you can get array of random number in shape of your choice you want

>>> import numpy as np
>>> np.random.random(1)[0]
0.17425892129128229
>>> np.random.random((3,2))
array([[ 0.7978787 ,  0.9784473 ],
       [ 0.49214277,  0.06749958],
       [ 0.12944254,  0.80929816]])
>>> np.random.random((3,1))
array([[ 0.86725993],
       [ 0.36869585],
       [ 0.2601249 ]])
>>> np.random.random((4,1))
array([[ 0.87161403],
       [ 0.41976921],
       [ 0.35714702],
       [ 0.31166808]])
>>> np.random.random_sample()
0.47108547995356098
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
6

This solution works!

random.randrange(0,2)

Hozayfa El Rifai
  • 1,432
  • 7
  • 17
infinito84
  • 1,971
  • 17
  • 8
2

RTM

From the docs for the Python random module:

Functions for integers:

random.randrange(stop)
random.randrange(start, stop[, step])

    Return a randomly selected element from range(start, stop, step).
    This is equivalent to choice(range(start, stop, step)), but doesn’t
    actually build a range object.

That explains why it only gives you 0, doesn't it. range(0,1) is [0]. It is choosing from a list consisting of only that value.

Also from those docs:

random.random()    
    Return the next random floating point number in the range [0.0, 1.0).

But if your inclusion of the numpy tag is intentional, you can generate many random floats in that range with one call using a np.random function.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
-9

My variation that I find to be more flexible.

str_Key           = ""
str_FullKey       = "" 
str_CharacterPool = "01234ABCDEFfghij~-)"
for int_I in range(64): 
    str_Key = random.choice(str_CharacterPool) 
    str_FullKey = str_FullKey + str_Key 
M T Head
  • 1,085
  • 9
  • 13