1. Dear Guest, if you are not receiving a forgot pw email then use the contact form at the bottom of the page, or email [email protected]
    Dismiss Notice
  2. Dismiss Notice
  3. Dear Guest, some forum sections are hidden for you. Register for FREE for unlimited access.
    Dismiss Notice

Randomisers in programing [Tutorial]

Discussion in 'Tutorials' started by JfN, Feb 15, 2015.

Share This Page

  1. JfN

    JfN Well-Known Member

    Joined:
    Jan 18, 2012
    Messages:
    4,844
    Likes Received:
    28
    Trophy Points:
    48
    Today I'm going to teach Prime Forums about PRN (Pseudo-Random Number) and TRN (True Random Number) generation.

    Disclaimer: The word 'random' in programming, and the measurement of how 'random' something is, is based solely on predictability. The word 'random' itself in its own dictionary definition is a paradox and has no practical use.

    I once said that 'nothing is impossible to do on computers' -  in terms of programming. I was quickly argued by the statement that 'generating a random number' is impossible. This thread is built primarily for people who would like to learn how (pseudo) random numbers are generated, but also will show arguments against this statement and show how a truly random number can be generated.

    The idea that random number generation is impossible comes from the fact that nobody can program a computer to just pull out a number out of nowhere. Computers can't do this because it hasn't got a brain like we do and can't just spit out random numbers as it's told to. Many intermediate to moderately advanced programmers understand the concept behind Pseudo-Random Numbers. The way Pseudo-Random Number generators work is by using an algorithm based on a certain set of variables/conditions - which can be as simple as time/date but also as complex as including system information, a set of pre-defined strings/numbers, and pretty much any other form of data/information.

    Workable example: Create a button in whatever programming language you desire, and add a text field next to it to display a number. Now, whenever the button is pressed - program your application/website to get the time in seconds, then divide it by 15, multiply it by 1.59 and add 2, then round up to the first whole number. Put the number in the text field. This is a very basic pseudo-random number.

    The reason it's a pseudo-random number is because it isn't actually random, it's just based on a algorithm of conditions unknown to the end user. The problem with using generators this simple is because it is pretty easy to exploit and take advantage of. Example: Habbo dices use a Pseudo-Random Number generator, which used to be solely based on time. When people realised the way it worked, they were able to exploit it and rig the dices. Since then Habbo have used a way more advanced number generator to prevent this.

    Now, a lot of programmers are unaware of the possibility of a True Random Number generation. Aside from the fact that it's uncommon, it can generally involve a very expensive setup that has to be regularly maintained. True Random Numbers are based not on programming but on actual physical events within or outside the system.

    The most simple form of TRN can be generated by timing of keystrokes or very sensitively measuring mouse events, and system events that a user has have no actual influence upon. For example, the total bits loaded into the RAM, the current processor load, or even the current I/O of the HDD. Some may argue that this still isn't truly random because it is derived from a certain known source, and the variation of numbers that can be generated is still limited to a certain amount (in the examples; the total size of your RAM would limit a TNR based on the total bits loaded into the RAM, the CPU capacity will limit a TNR based on processor load, and the rpm of your HDD will affect a TNR based on HDD I/O).

    The next most simple level of True Random Number generation can be done using Atmospheric Processing (depending on how simple/random you want it). This is basically using events/conditions within the atmosphere to create random numbers using algorithms. This could be (at the most basic levels) sound, temperature, light, or any other physical events. The main thing that sets this aside from the first example is that no other source will be able to generate the same random number. This is what makes it truly random. If you put two very sensitive inputs beside one another to measure a certain atmospheric property, neither will generate the same number because of the way sound/light/etc travels, reflects/bounces/distorts(/etc) in physics.

    Although Atmospheric Processing can be simple, it can also become very complex by measuring sea movement, very sensitive land mass movement, weather, and even solar energy (these examples would make it extremely random).

    The most complex form of TRN I'm currently aware of is Radioactive Processing. There is absolutely no way (or at least it's extremely far from our current capabilities) to determine when a radioactive substance will emit particles, so you can use this to make extremely random numbers by detecting particles let of by these substances.

    For most websites and applications, a Pseudo-Random Number is good enough, and is the most commonly used. TRN's are only used when it's absolutely necessary.

    That is how pseudo-random numbers work and proof that truly random numbers are possible. Feel free to ask questions.

    Examples of PRN generators:
    AS3 - http://primeforums.net/Thread-AS3-Pseudo-Random-Number-Generation
    PHP - http://primeforums.net/Thread-PHP-Pseudo-Random-Number-Generation
     
  2. Ally Mac

    Ally Mac -

    Joined:
    Nov 11, 2014
    Messages:
    449
    Likes Received:
    0
    Trophy Points:
    0
    Interesting points, although I'm not sure I fully agree with the point about TRNs actually being truly random, maybe close too, but not quite perfect. It is entirely possible to replicate the same results and afterall they are based on an algorithm, and could be exploited (MTM attacks, maybe?). I would argue that computers, for now, will never be able to be random.

    With TRNs if you have a large enough sample size (and this may haver to be trillions large) you will find the pattern, so they aren't truly random but just random enough that it doesn't matter.

    A bit pedantic I guess though! Nice thread.
     
  3. JfN

    JfN Well-Known Member

    Joined:
    Jan 18, 2012
    Messages:
    4,844
    Likes Received:
    28
    Trophy Points:
    48
    Thanks for the feedback dude!

    The part about atmospheric processing was made to point out that two individual inputs cannot detect the exact same measurements due to differences in space and travel of whatever energy (sound/light/etc). The slightest mm or angle (°) will affect the machines interpretation of the source. This, of course, is a case where extremely sensitive devices would be required to get the most accurate result.

    Although in a very specific case you'd be right, two machines using the same source of input could replicate the same results, but in any situation where TRN's were absolutely necessary, a dedicated machine would most likely be involved.

    Again, thanks a lot for the feedback, glad you liked the thread :).
     
  4. Luke

    Luke -

    Joined:
    Feb 9, 2011
    Messages:
    2,123
    Likes Received:
    0
    Trophy Points:
    36
    Very informative and education. Nice thread. Would love to see more similar threads.
     
  5. JfN

    JfN Well-Known Member

    Joined:
    Jan 18, 2012
    Messages:
    4,844
    Likes Received:
    28
    Trophy Points:
    48
    Thanks for the feedback :) I'll write more when I can think of more stuff to write about :p.
     

Top