Turing Logo  

Designed for computer science instruction, Turing is simply the easiest, most fun, and most effective way of teaching programming concepts.


Quick Links
Home page of Holt Software Associates  | Home page of the Turing Programming Language, the fastest way to teach programming concepts  | Home page of Holt Software's Java products  | Home page of Ready to Program with Java(tm) Technology, a Java development environment designed for education  | Information about Holt Software's courses for teachers  | Information about how to contact Holt Software  | Information about how students can purchase Holt Software's books and software  | Information about how schools and bookstores can purchase Holt Software's books and software

Random Numbers in Turing


  1. How does Turing create random numbers?
  2. Where does this seed seed come from?
  3. What if I want Turing to use the same sequence of random numbers each time (for testing purposes)?
  4. I'm interested in all the math about random number generation. What's a good reference?

  1. How does Turing create random numbers? The Turing (and almost any other system) starts with what is called a random number seed. Whenever you program requests a random number using randint, rand, or any subprogram from the Rand module, Turing performs a fairly complex calculation on the old random number seed and produces a new one. It then takes this new seed and uses it to produce the number requested.

    For randint and Rand.Int, it essentially does a mod of the new seed with the range of values for specified in the randint call. For rand and Rand.Real it returns a value that is between 0 and 1 by dividing the new seed by the highest possible value for the seed. The next time a random number is requested, this operation is repeated. Each time a new seed is created.

    Note that there is no random element here. If you have a given seed when making a request for a random number, the next seed will always be the same. In other words, random numbers aren't random. It's just that the user doesn't know what the next number will be. For this reason, computer scientists call them pseudo-random numbers.

  2. Where does this seed seed come from? There are no actual random parts of a computer. (Or at least there shouldn't be!) You also can't make something random from something that isn't. You can, however, make it appear to be random. The way this is done is by selecting a seed that changes every time that the program is run and never (or almost never) repeats itself.

    The only candidate for that is the system time. (Which is what almost every random number generator uses to create a seeds. Each time a Turing program executes, Turing takes the current time (in number of seconds since Jan 1, 1970) and manipulates the bits to make the the seed for the random number generator.

    You can see this in effect if you run the program

            for i : 1 .. 5
                put Rand.Int (1, 100)
            end for
    several times in a row (press F1 multiple times). If you are running it more than once a second then you will see the same random number sequence show up. Each second the sequence will change as Turing generates a different seed when the program is executed.
  3. What if I want Turing to use the same sequence of random numbers each time (for testing purposes)? You can set the seed to a preset number by using Rand.Reset. Note that you will get the same sequence of random numbers, not the same random number each time you call Rand.Int.
  4. I'm interested in all the math about random number generation. What's a good reference? A complete and thorough (and heavily mathematical) explanation about random number can be found in that bible of computer programming,
            The Art of Computer Programming, Volumes 1, 2, 3
                         by Donald E. Knuth
                     Published by Addison Wesley
       
    In my humble opinion, this series of books are perhaps the quintessential reference work for computer scientists.

[ Turing Home ] * [ Top of Page ] * [ Feedback ]