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

Username:   Password: 
 RegisterRegister   
 Random Integer help!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bvbjules13




PostPosted: Wed Oct 31, 2012 3:25 pm   Post subject: Random Integer help!

What is it you are trying to achieve?
i am trying to have 25 variables each have a random number between 1-100


What is the problem you are having?
if i use one variable with the value Rand.Int (1,100), each time time i use the variable will the random number be the same or will it be different each time it is called/used


Please specify what version of Turing you are using
4.1
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Wed Oct 31, 2012 3:59 pm   Post subject: RE:Random Integer help!

If you assign a value to a variable, does it change each time your read/access it?

Rand.Int (1,100) is a function call that will generate and return a pseudo-random number.

code:
var num := Rand.Int(1, 100)


Turing interprets the above as:
It will call Rand.Int(1, 100). Eg: say it returns 34.
code:
var num := 34


Then it will define the variable num as the int datatype as Turing assumes your variable's data type must be an integer as that is the type you're trying to use with it.
code:
var num :int := 34


You are assigning a number value to the variable num. You are NOT assigning the function to another name. You will be returning the value you assigned to it when you call it. You will not be calling Rand.Int(1, 100) as if it were a shortcut if you call the variable. The latter is possible however (in other languages if not Turing), but that's for another lesson.
mirhagk




PostPosted: Wed Oct 31, 2012 4:13 pm   Post subject: RE:Random Integer help!

If you are using 25 variables it may be time to look into arrays. Be careful not to take on too ambitious projects before you learn the required concepts.
chipanpriest




PostPosted: Wed Oct 31, 2012 5:44 pm   Post subject: Re: Random Integer help!

If you have a variable with a value of Rand.Int (1,100), that variable will choose a number, and stay that number. For example:

Turing:

var randomNumber : int := Rand.Int (1,100)

put randomNumber
put randomNumber
%Both numbers will be the same


To have 25 different variables you could either make 25 different variables:
Turing:

var number1 : int := Rand.Int (1,100)
var number2 : int := Rand.Int (1,100)
%etc...


Or, you could make an array, which is easier to keep track of and will make your program less cluttered and have less lines of code:
Turing:

var number : array 1 .. 25 of int  %This makes the variable
for i : 1 .. 15 %Regular for statement
number (i) := Rand.Int (1,100)  %As I increases, so does the subscript in the array, and you give each subscript a value of Rand.Int (1,100)
end for

If you don't understand this array stuff, I'm sure there's a tutorial on arrays.
bvbjules13




PostPosted: Wed Oct 31, 2012 9:34 pm   Post subject: Re: Random Integer help!

Thanks you guys! definiftly helped, i just ended up creating 25 variables with random integers ^.^ my course wont let me do it another way
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: