Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Sample] Basic Random Numbers
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: Wed Oct 06, 2004 7:05 pm   Post subject: [Sample] Basic Random Numbers

scroll down

hi, i don't know if there is already a tutorial like this but i'm going to post it anyways to help teach the nubes. lol
whenever you want to use random numbers in turing you have to use the word randint and put the numbers you want turing to randomly pick out from. below is an example of what i mean. i put some comments in for you to understand exactly why i put some things. i hope you learn from this. enjoy!

code:

%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

Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Oct 06, 2004 8:20 pm   Post subject: (No subject)

randint is a preaty bad way to do it Confused You're better off with Rand.Int(low, high)
code:

for i : 1 .. 10
    put "a random number between ", -i, " and ", i, " is : ", Rand.Int (-i, i)
end for
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Delos




PostPosted: Wed Oct 06, 2004 8:27 pm   Post subject: (No subject)

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:
code:

var number : int
randint (number, 1, 10)

Here 'number' is assigned a value from 1 to 10.
One could also say:
code:

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():
code:

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:
code:

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




PostPosted: Fri Oct 08, 2004 1:19 pm   Post subject: (No subject)

sorry, my bad. didn't realize i wrote the code wrong. Rolling Eyes
Tony




PostPosted: Fri Oct 08, 2004 6:34 pm   Post subject: (No subject)

that's ok - you tried Smile
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: