I want generate a random number between [a,b] inclusive in python
I should be using random.randint(a,b)
but what if I only remembered random.random()
, which returns a random float [0,1), can I still get an uniform distribution of random numbers?
my thoughts are
round(a + random.random()*(b-a))
I did some test with this…result looks correct, but I am not confident that this is mathematically/statistically correct- take the last few bits, something like
random.random() & 0b1111
(since the resulting float is supposed to be random set of bits, I am taking a page from here)…but I didn’t find a way to convert float to binary or do any bit manipulation on it in python