
-----------------------------------
cool dude
Wed Oct 06, 2004 7:05 pm

[Sample] Basic Random Numbers
-----------------------------------

%Guessing Game
var number, guess, left : int   %stores the answers the user gave and output.
loop
    randint (number, 1, 10) % you have to put this because you want the program to randomly choose a number 
    left := 3   % Displays the amount of guesses left
    put "I have picked a number between 1 and 10. You have 3 guesses to guess it!"
    put " "
    for a : 1 .. 10
        put "Enter your guess."
        get guess
        if guess > number then
            put "Your guess is to high."
            put "You have " ..
            left := left - 1
            put left ..
            put " guesses left."
            put " "
        elsif guess < number then
            put "Your guess is to low."
            put "You have " ..
            left := left - 1
            put left ..
            put " guesses left."
            put " "
        elsif guess = number then
            put "You are correct!!!!!  Good guess!!"
            exit
        end if
        if left = 0 then
            put "Nice try! The number is ", number
        end if
    end for
    put "Would you like to try again (y/n)?"
    var again : string
    get again
    if again = "n" then
        exit
    else
        cls
    end if
end loop



-----------------------------------
Tony
Wed Oct 06, 2004 8:20 pm


-----------------------------------
randint is a preaty bad way to do it :? You're better off with Rand.Int(low, high)

for i : 1 .. 10
    put "a random number between ", -i, " and ", i, " is : ", Rand.Int (-i, i)
end for


-----------------------------------
Delos
Wed Oct 06, 2004 8:27 pm


-----------------------------------
Hmm...
Well, I guess since it was for "extreme newbies"...

First off, in the future please check your code before you post it.  This code was buggy...your for loop does not work correctly.

Random Numbers, eh?

Random numbers in Turing, as in (just about, if not) all programming languages are not really random.  They're just generated from a list so they seem random.

As mentioned, one has the randint() routine.  This randomizes an integer.  Thusly:

var number : int
randint (number, 1, 10)

Here 'number' is assigned a value from 1 to 10.
One could also say:

var number : int
number := Rand.Int (1, 10)

and the same would be true.

So we have integers, what about real numbers?  We use rand():

var realNumber : real
rand (realNumber)

Note that one does not specify a domain for the function, the variable is automatically assigned a value from 0 - 1 (to six decimal places).
One could also use:

var realNumber : real
realNumber := Rand.Real

with the same effect.

There are a number of other Rand.() functions available, but these deal with situations in which strings of random numbers are required, but the same sequence needs to be generated each time.  So, for 'extreme newbies', there is no need to worry.
If in doubt, press F10!  Muwahahahaahaha.

ARGH!  Beaten to the post by tony...meh

-----------------------------------
cool dude
Fri Oct 08, 2004 1:19 pm


-----------------------------------
sorry, my bad. didn't realize i wrote the code wrong. :roll:

-----------------------------------
Tony
Fri Oct 08, 2004 6:34 pm


-----------------------------------
that's ok - you tried :)
