Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
from time import time

def baseRandom():
    return hash(str(time()))

def randFromZero(maximum):
    return baseRandom() % maximum

def randRange(minimum, maximum):
    return randFromZero(maximum - minimum) + minimum

I ask what's wrong because it seems so simple and stupid. This is something I just thought up one day and put together in 5 minutes, but it seems to generate even and unpredictable results. I've done a couple of simple tests like generating a lot of numbers and looking at the variance between the amount of times each number is generated, and using it to create a little grid which has tiles that are either black or white with equal probability, in which I saw no clear patterns. Hopefully someone who knows a thing or two about this can educate me.

share|improve this question
How are you intending to use the "random" output? Why aren't you using the builtin random functions? – JeffS Apr 25 '14 at 19:24
@JeffS I'm not intending to use it and always use the built-in functions. This is just a bit of fun. – user1149589 Apr 25 '14 at 19:35
The time() function returns quantized time; the least significant digits of the time are "chunky" and usually represent a small subset of available numbers. Hashing the string will introduce more clumpiness; ASCII strings tend to be very sparse, and you are taking the string of a number. The hash function will map to a very small portion of the integer space. Now, the modulus of a small portion of the integer space is an even smaller portion. – Bob Dalgleish Apr 25 '14 at 19:50
Worth saying, that (although small) there is a chance that if you run this program on two computers with synchronized clocks they will both generate the same numbers. You could (just for fun) append the PID to what gets hashed. Also, if someone wanted, they could use it to generate the entire series of possible numbers your program could have used in a given time period. It looks a little like you have written a poorly seeded PRNG :), consider (for fun) adding more sources of randomish data to the hash input. – IanNorton Aug 7 '14 at 6:20
up vote 8 down vote accepted

It looks like you're relying on running this on a system where the granularity of the time() function is smaller than the amount of time it takes to execute baseRandom(), do what you want with the results, and come back to call baseRandom() again. If not, you'll get more repeating numbers than you should. You're also relying on the result of the hash() function to be sufficiently pseudorandom given the input you're feeding to it.

Detecting an obvious pattern depends on how you look for it. I think Knuth gave an example of a RNG that looked OK when you plotted pairs of numbers on an x-y plane, but if you took three numbers in a row as x-y-z coordinates, and look at the resulting cube from the right direction, you can clearly see a finite number of planes where all the outcomes lie. In other words, just because you tried a couple of things and didn't see a pattern doesn't mean a non-random pattern isn't there.

There are fifteen tests for randomness listed here, if you're interested:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.