The most possible True Random Number Generator

We know that our physical world is deterministic (and please, don’t come with the Dr Quantum videos of YouTube, by more strange and successful that the QM laws are, they do not fully represent a solid understand, and there are still many gaps to be filled), which means that creating a TRUE random number generator it’s impossible. Some people uses atmospheric noise to create random numbers, but it’s just a hard-to-predict approach and not a TRUE random number generator, there are some people using the radioactive decay too, but it can still be just a unknow mechanism. When I say by “TRUE”, I’m talking about a number that is created by something that could break the causality law, something that have an effect without prior cause. So, I think, the secret for creating a True Random Number Generator (TRNG) instead of a Pseudo-Random Number Generator (PRNG) is to link our generator with the only thing that is supposed to break the causality law: us.

We have two possibilities, the first, is that we have free will and the other is that we are predetermined since the begin (if it had a begin) of the world or even before. So, if we have a free will, we must have in some way, to break the causality law, otherwise, it is impossible to have a free will if we follow the causality law. So, any RNG which is linked with a human created content is, philosophically, linked with our free will too.

But, how can we create Random Numbers using a user generated content ? Well, one way is to use Twitter Public Timeline to create random data, since it’s a random stream of data which is linked with our free will, we can use other stream of user created data, but for sake of simplicity I’ve used Twitter Public Timeline =)

So, what I’ve done was: subclassed the Python Mersenne Twister random number generator to get the Twitter Public Timeline and then reseed the Mersenne Twister in every call of the random() method. Here is the source-code:

import random
import time
import twitter

class TwitterRandom(random.Random):
   TOO_SOON = 1
   ITS_OK   = 0

   def __init__(self, seed_value=None):
      random.Random.__init__(self, seed_value)
      self.twitter_api  = twitter.Api()
      self.twitter_data = []
      self.last_time    = time.time()

   def random(self):
      if len(self.twitter_data) <= 0:
         if self.load_twitter_data() != self.ITS_OK:
            return random.Random.random(self)

      return random.Random.random(self)

   def load_twitter_data(self, check_time=True):
      if check_time:
         if (time.time() - self.last_time) <= self.TWITTER_CACHE_TIME:
            return self.TOO_SOON
      data = self.twitter_api.GetPublicTimeline()
      for status in data:
         vals = [ord(v) for v in status.text]
      return self.ITS_OK

def run_main():
   true_random    = TwitterRandom()
   random_numbers = [true_random.randint(0,100) for i in xrange(10)]
   print random_numbers      

if __name__ == "__main__":

Unfortunatelly, Twitter caches the Public Timeline for 60 seconds, so when using the Random Number Generator, it can run out of user created data.

And of course, this is just a philosophical speculation, based on the fact that our intuition says that it’s more possible to have free will and something breaking the law of causality in ourselves instead of in a radioactive decay or in the world outside us, since we’ve never saw something breaking the law of causality in the external world.

36 thoughts on “The most possible True Random Number Generator

  1. Quantum mechanics says that an electron decays from a high energy orbit to a low energy orbit WITHOUT A CAUSE. Various mathematical/physical evidence very strongly indicates there can be no underlying cause of any kind, i.e. they rule out all explanations that have any kind of hidden variable. Google for “Bell’s inequality”.

  2. I know about the Bell’s Theorem and about the local hidden variables, but they do not even have well explained the double slit experiment. Read the EPR paper and you’ll understand why I can’t understand how the Copenhagen interpretation was so well accepted. There are many evidences, you’re right, but all with the bizarre explanations and no real proof yet; they always put a strange theory instead of saying that we simple do not know yet the mechanisms of the small particles.

  3. Very cool idea Perone!

    Though, here’s one thought: Twitter is prone to massive spikes in topics centered around a small set of topics (e.g., #iranelection, #mj, #followfriday, etc). Won’t a random number generator of this kind be prone to non-randomness given these inherent patterns in the public timeline?

  4. Is there an application of random number generation where the point is not to pick a guessable number? If an attacker knows (to within the last 60 seconds) when you generated the number, what improvement does this offer?

  5. @drew thank you man, to overcome this problem, I’ve used the reseed of the Mersenne Twister, which has a period of 2**19937-1. I’ve not created a complete new random generator, I’ve just added the Twitter user created noise to the Mersenne Twister generator.

  6. @Ryan, that’s the point Ryan, look at the Public Timeline, there is no time, just “[sometime] ago”. And you can’t get too much older statuses of the Public Timeline. But you must think that doesn’t matter if it is Twitter or another user generated stream, the main ideia is to use human generated data to reseed the Mersenne Twister, this data can be any kind of data linked to our free will, it could be an ambient sound noise in a place with many people for example, do you understood ?

  7. There is not an underlying deterministic mechanism in quantum mechanics – this is ruled out by Bell’s inequality as kanzenryu points out, and as many very detailed and accurate experiments including studies of photon polarization confirm. So something like atomic decay is going to be inherently more random than twitter which has cycles and trends of words.

  8. With the twitter api, I can pull the public timeline for any 60 second period.

    On the main topic (using humans are the seed), humans are phenomenally bad at behaving randomly, or telling the difference between random and nonrandom behavior (example: iPod shuffle). They seem like a bad choice for this reason.

  9. @Ryan: Anyway Ryan, even if you can predict twitter timeline, you could use any other human generated stream of data, it could be human sound noise, which is a GREAT stream of data, etc… it’s not hard to imagine how man human created streams of data we can get, that’s the main idea.

    @Ben: we can’t even know what is going on inside the atom, how can you assume that atomic decay violates the law of law of causality ? Please note that I’m not saying that it is impossible to atomic decay to violate the law of causality, what I’m saying is that we simple don’t know the mechanisms yet, and we are assuming things that we can’t assume.

  10. Hi Perone!

    One question: did you study qm physics or did you just read some paper and google your knowledge?
    I do not want to deny your knowledge or your experience, but simply saying:
    “TRUE random number generator it’s impossible” is a bit naive. With this sentence and without any deep knowledge about physics this sounds a bit arrogant to people who studied physics not only for some days or months or years …

    Another question for me is: how could twitter then be undeterministic if even (the underlaying) physics is deterministic? (I assume that biologic processes are determined by physics …)


  11. Hi Peter, thanks for the comment. Peter, the assumption that a TRUE random number generator it’s impossible is based in a prior assumption that our world is deterministic. Nobody knows yet if our world is deterministic or not and I don’t know it too and even Einstein knew that. So, to me, this doesn’t sound arrogant, since I’m not claiming that I know that something is possible or impossible, I’m doing assumptions based on prior assumptions. The fact is simple to understand, I’m saying that the “most possible” TRUE random generator must be linked with our free will, because our intuition says that the only thing in the world which seems to be truly random is our free will, have you saw something in the world (except in Quantum Mechanics which is still full of gaps and without a decent philosophically base) which violates the causality law ? I’ve not seen it yet, and the only thing that makes me thing that could break the causality law, is our free will (IF, AND ONLY IF, it breaks the causality law). So, did you understood ? It’s not arrogant, since I’m not claiming that I know the answer, it’s just a philosophical speculation, don’t you think ? I’m not a physicist, but I’m don’t need to be to understand things, like you don’t need to be a Computer Scientist to comment in this blog. It’s true that the atomic decay can be really random, like our free will, but the question here is that we can’t even know what is going in the atom, YET, but maybe in the future we can prove this, or not.

  12. Hi Perone!

    Who says that our will is free? Can you proof this YET?

    Maybe your assumption is true that the world is deterministic (this is not the current believe in the “state of the art physics” – very interesting topic/discussion!!).

    But you cannot argument that physics is deterministic and then we have to choose a less proofen area to get real randomicity…

  13. Our intuition says that our will is free, but of course that no one can prove, and probably will never prove. You must understand that no one can prove that the world is deterministic or non-deterministic, because we always get the “but if it’s something that we do not know yet ?”. Because of this I argumented that the most “possible” true random generator will be linked to our free will, I know that I can’t prove that our will is free, but if you put the real world and our free will side by side, I will vote on free will, because in the real world I’ve never seen the causality law violated, but in free will, my intuition says that it could be possible, unless if we are not free.

  14. You cannot prove that the reality *will* be deterministic, but you can prove that at least the current thread of the universe was determined, meaning up until now the universe has been deterministic.

    True randomness might be a lie, but perhaps we can change the definition of true randomness to “unknowable without having a perfect universe simulator”. Sorry, mine broke.

  15. @DeadWisdom, right man, but what you think that it is “most possible” to break the causality law: what we have seen until now (a deterministic world) or our free will (that we don’t know) ?

    We have two possibilities:
    1) our will is free
    2) our will is not free.

    If our will is free, we have a truly random number generator. If our will isn’t free, why the world will be and why the hell we need a true random number generator since we aren’t even so.

  16. Hrm, I’m not sure I get all that you are saying. But I will say this:

    Our will is free, and the universe is deterministic. They are not mutually exclusive. We, you and I, are machines in the ether of reality. But that does not make us less so; I assure you love is real; our notions of ourselves are concrete.

    Email me if you want me to explain more… I should just write a book or something.

  17. @DeadWisdom, how they aren’t mutually exclusive !? When I’m saying that “universe is non-deterministic”, I’m saying that at least something in the universe is non-deterministic. If we break the causality law, we are non-deterministic, as we make part of universe, this makes the universe non-deterministic. If you like those question, I recommend you the Kant’s “Critique of Pure Reason”, it’s a must-read book for philosophy lovers.

  18. Free will does not imply non-determinism. You are taking that as granted, and it isn’t. Free will means we get to choose what we want, but whereas you might define “choice” as arbitrary decision, I define it as following a logical pattern.

    You might say that this is not free-will, that following a logical pattern means that you don’t actually get to decide. But the mechanism of decision is built into our very being; we cannot separate it from our consciousness, and so therefore it is just as real as anything else.

  19. DeadWisdom has a point, any “random” choise means acting against your interest. Normally, logic dictates just one optimal choice, so what choice do we have?

  20. Thanks for the comments =)

    @DeadWisdom, your concept of “free-will” doesn’t imply non-determinism, what you’ve said it’s the notion of a “deterministic will”. You’ve just changed the cause of will to our very being, into the decision mechanism. But, this decision mechanism, what is ? It is deterministic ? It’s free ? Where are the fundamental cause that makes we decide to want something or not ?

    If the free will doesn’t imply in a non-determinism, we are predestined since the begin of universe (if it has a begin hehe), it’s a consequence of your assumption, and it’s a rational consequence I think, don’t you ?

    @makc, we have the choice to decide what is our interest hehe =) if the world is non-determinsm of course, in my point of view. There is an interesting point by the philoshoper Schopenhauer in a book, in which he notes that Seneca admirably says: “velle non discitur” (Willing cannot be taught), meaning that the will can only be affected by motives, but these can never change the will itself. However it’s a very long discussion that doesn’t fit in a comment box hehe.

  21. in the time of Schopenhauer they thought the will is part of nature, not just our brain. poor idealists, they did not even thought atoms were real…

  22. Getting data from Twitter is random in the sense that humans have affected the data. But then humans effect the weather too (and in ways more subtle than “global warming”), so weather data is (philosophically speaking) no less random than Twitter.

  23. @James, there are currently infinite ways to get random data using weather, it depends on what kind of method you use and what kind of “weather” data you collect, if you’re absolute certain about the human effect in the weather data collected and has enough entropy, it can be truly random, otherwise, you’ll fall in the uncertainty. How to prove that human truly affects with his hill the weather data you’re collecting ? If you prove, you’re right.

  24. If the term random refers to a phenomenon that does not result from any determined cause then many phenomena that the world considers random today and for centuries should not be considered random. For example, rolling a dice is considered to give a random result. However, rolling a dice is determined by many things: The force with which it is thrown, the position of the dice the moment it is released, the angle it is thrown, the force and rotating angle of the spin it is given when released, the surface where it lands, air components and probably more. If dice were to be rolled using some mechanism by which all the above parameters were always exactly the same and unchanged then the result would always be the same! In which case, rolling a dice should not be considered random.

    However, in my opinion, it is not important if randomness is deterministic or undeterministic but whether its result can be in any way predicted or not; and in cases where you have a series of results, whether they are unbiased and do not follow a discernable pattern.

  25. Hello Michael, yes, the physical determinism says that if we know all the variables, forces and initial position of the dice, we can preview the result; of course it’s too complex to do, but if you throw up a ball, you can preview the time in which it will hit the floor if you know all the forces, position, etc… that’s why we call the random generators by “Pseudo Random Number Generators” (PRNG), but what I’m talking here is from a generator that it’s not simple just too complex to foresee, but it is impossible to foresee. This is a philosophical speculation, it’s important just for philosophy, and not so much for reality, just a interesting thing (however a True Random Generator could be very useful for crypto). But for simulations in real life, or some crypto methods, a “hard to predict” approach you cited is enough =)

  26. I was looking at your code, what I cant understand is what do you seed at the marsene twister, do you seed the sequence of posts in the public timeline? or you get all of the posts and seed them?


    also if i set this to true i get a big stream of random numbers, is that beacuse I seed every post in the timeline?

    I am kind of confused about how the code works!

  27. Hi Perone,

    I’m just googling for a “true random” generator, and i found your page… sorry my english is not perfect :-)

    I think the world is non-deterministic, and a proof exists.

    All system that is deterministic can be described as a kind of very complex machine. A very-complex machine should work as a very complex logical system. According to Gödel’s incompleteness theorem: in a logical system one or more statement included without proof… This means: inside any complex logical system we can’t be sure about anything, because we can’t deduce any statement to a solid base.

    So my question is? How do know that you are exists? You can’t be sure about it (nor just a moment) if you are just a machine. (personally I’m sure that I’m exists :-)

    I think your intuition is right. The freedom is inside us.

  28. I was researching the possibility to use randomness to predict certain events.

    There are no random numbers, not in radioactive decay not in noise, nowhere. Everything is just hard, or very hard to predict. If you are missing data, they are harder to predict (such as using light to generate randomness).

    This is because there is no random within a closed system. A closed system is philosophically speaking balanced, leaving no space for randomness if seen as a total. Random numbers are well distributed, making me think that you can’t predict them but you can predict what they wont be.

    The only TRUE random source should be from outside the system, from an other system, and the only such source i know of is life itself.

    Using human generated content is “true” random, but the twitter method is not clean, so its unusable for my experiment.

    I will perform a social experiment on a community i run , hopefully with a more clean random input.
    The only true random human input would be to put a bunch of people hit randomly a on off button, and thats too subjective on a small group. To do this on a large group of people i will have to go sligtly different.

    I think there is huge potential in randomness, it can be used to filter out nonrandom things and then group those together to exclude and optain whatver you want to predict.

    If you are interested in a discussion about this or similar contact me.

  29. The existence of true randomness (uncaused events) bothers me a great deal, and I personally do not believe in its existence. That said, simply ignoring the experimental evidence for its existence won’t get us anywhere.

    That said, one could look into non-local hidden variable theories. Only local hidden variable theories have been discounted. Non-locality is normally seen as a huge drawback to a theory, but if you’re willing to accept that, then the Bohm interpretation (or something like it) can allow QM to coexist with determinism.

    Now, that said, as someone who has worn all four hats at one point in time or another: cryptographers, statisticians, physicists and philosophers mean subtly different things by the word “random”. (Much more qualified as a statistician or cryptographer than physicist or philosopher).

    When a cryptographer wants randomness, they really want a stream of data such that is impossible (or at least prohibitively difficult) to predict from any source of information available to the adversary. This covers previous sub-parts of the stream, other known seeds, times of generation, or any other such information.

    When a statistician wants randomness, they want data that obeys a particular distribution (uniform is commonly desired, as is normal). Also, they want the “random” stream to be independent of any variables they are studying/observing. It’s not so important that the stream be unpredictable (hence why Statisticians love Mersenne Twisters, but crypto-geeks generally do not).

    When a physicist wants randomness, they (usually) are referring to an event without a cause. This most commonly appears in the realm of QM.

    Finally, philosophers generally enjoy playing games with words by switching definitions of “random” mid-argument :-). The definitions they use are at least as numerous as the discussions they have.

    So, if you would like to generate very good randomness, the first step is to decide which kind of “random” you want. If you want to generate one-time pad keys, better to rely on QM or physical noise (people may or may not be entropy sources, but they are also great sources of side-channel attacks not related to the quality of your methods). If you want to run simulations, just use straight Mersenne Twister seeded from they current time. If you want to discuss physics or philosophy, then have fun – I’m out.

    1. Im confused… horribly confused. If an event can occur without a cause is that not stating that energy can be created from nothing?
      (Every action causes an equal re-action and so on)
      Isnt everything cause and effect?

      Therefore randomness must be impossible in the world of cause and effect. The same set of inputs under the exact same conditions will always generate the same output. And inputs were previously the outputs of another condition.

      In order to generate randomness wouldnt we have to look beyond and step out of the world of cause and effect.

      Clearly i do not know anything about philosophy or mathematics or statistics, this is just my strange ramblings. hahahah

  30. If you ask yourself who am i you cannot answer the question… you will simply have an answer which will come back to the question. Every cause has an effect. There is no end when viewed from that perspective.
    question. Who am I?
    answer. Stephen.
    question. Thats my name, but who am I that has this name?
    answer. me this body.
    question. Who am I that has this body?

    My point is that we cannot get the answers physically as the physical world is bound to cause and effect. Every question creates another set of questions…
    Im possibly losing the plot as I have no clue now what im talking about….

  31. An interesting article, but one I assumed to be a joke before I read the comments.

    It is true (and not arrogant) to say that IF the world is deterministic then “true randomness” is impossible (taking the effect without cause definition). But sadly you seem to believe that a universe (that is EVERYTHING) can be entirely deterministic and still contain elements which are not deterministic (people). Clearly you cannot have it both ways. It is impossible to observe the world without affecting it (see quantum physics) and if we observe based on free will then our non-deterministic effect will have “random” ripples through the world. Therefore if we are non-deterministic and we exist in the world then the rest of the world must, to a certain extent also be “random”.

    Which leaves the matter of your opinion:
    We are the only random element in the world.

    … While it is nice to feel unique (it gives a feeling of purpose in life), you do little to suggest any evidence for this being the case. The view seems entirely based on logic which could be summed up as “scientists say that nothing is random but I feel random … so I must be an exception but the scientists must in general be right”. Might I suggest that it would be a better assertion that your feelings indicate that scientists are wrong. That is; either you are right to say and you are random, or scientists are right to say that nothing is random. Both things cannot simultaneously be true because we are part of the EVERYTHING that scientists say is deterministic.

    You offer no other evidence for this apparent “uniqueness” and without it your whole argument fails.

    On the side of evidence that suggests we are not unique…
    There are numerous studies which have shown just how predictable we are, not to mention easy to manipulate (control [with cause and effect]) we are. You only have to look as far as some of the stage shows of “Darran Brown” to realize just how little we do that is even remotely non-predictable.

    From my own philosophy I would suggest that the reason we perceive ourselves as being non-deterministic is because we cannot fully conceive the cause / effect relationship of our own decisions. This gives rise to the retrospective view point that our previous actions must have been carried out without deterministic logic. This seems a more likely explanation than “I must be the only thing that’s random in the universe”.

  32. Energy in our universe is finite. It is just transforming from one type to another, to matter and back. It all started at the Big Bang with a Singularity. We don’t understand singularities very much. We do know that laws of physics break down. So the seed (singularity) of our random algorithm (universe) is unknown, hence if that remains unknown and completely out of our reach, you could say we live in a truly random universe. The things we can predict are just clumps of chance where the probability of event A happening are very high vs Event B.

  33. Random no are a sequence of numbers which can not be predicted on the fly.
    With the help of certain random number generation techniques we can just generate “PSEUDO RANDOM NUMBERS”.

Leave a Reply

Your email address will not be published.