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

Username:   Password: 
 RegisterRegister   
 question about a data file
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
meidan




PostPosted: Tue Jun 13, 2006 7:50 pm   Post subject: question about a data file

lets say i created a datafile which contains values like:
0.01
1
5
10
25
50

and so on...

is there any way i can output these numbers randomly? i tried couple of ways but none of 'em worked...

if someone can help me then plz do so...

thx in advance
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Tue Jun 13, 2006 8:03 pm   Post subject: (No subject)

You could read all the lines into an array and then pick a random element from the array.
meidan




PostPosted: Tue Jun 13, 2006 8:14 pm   Post subject: (No subject)

Cervantes wrote:
You could read all the lines into an array and then pick a random element from the array.


can you be a lil bit more detailed cuz im not really familiar with this command and i need this problem solved for tommorow so if you can help me here i will really appriciate it..
Clayton




PostPosted: Tue Jun 13, 2006 8:20 pm   Post subject: (No subject)

here is an example in pseudo code
Pseudo:

create an array to hold integers
open the file
get all contents from the file and put them into seperate elements of the array
randomly pick one element of the array to ouput to the user

simple as that, check into the Turing Walkthrough for use on arrays, and file I/O Very Happy
Omnipotence




PostPosted: Tue Jun 13, 2006 8:24 pm   Post subject: (No subject)

What you want to do is do Rand.Int (1, number of values in your datafile)

Lets say you had 5 values and Rand.Int got you the number three. You could for loop getting from the file. Lets say you used the variable woot to save whatever value you wanted. You would loop it three times to get from the file to woot. Due to the nature of getting from a file, it would get one line at a time so then once it reached the third line it would store whatever value was there to woot. Then you could use it for whatever you wanted. So using the Rand.Int, you would go and get a random line each time.

What the people above are suggest is that you get everything and save them into x # of variables using arrays. (Read arrays tutorial) then you would Rand.Int and call on one of the variables.

variable_name (Rand.Int (1, ?))
Cervantes




PostPosted: Tue Jun 13, 2006 8:27 pm   Post subject: (No subject)

Omnipotence wrote:

Lets say you had 5 values...

Of course, this means we know how many lines are in our file. Turing can't know this without reading through the whole file. Wink
meidan




PostPosted: Tue Jun 13, 2006 9:11 pm   Post subject: (No subject)

ok i read the array guide and now i know what this is. I realize it is very useful command but i still cant seem to figure out how to output 1 of the numbers from the array randomly

I tried using randomize and then an if loop but it does not give me the number randomly...it gives me the same first number in the array decleration.

any suggetions?
Cervantes




PostPosted: Tue Jun 13, 2006 9:41 pm   Post subject: (No subject)

code:

% lines is an array from 1 to 5 of string
put lines (Rand.Int (1, 5))
Sponsor
Sponsor
Sponsor
sponsor
Bored




PostPosted: Tue Jun 13, 2006 9:44 pm   Post subject: (No subject)

Extremely simple.
code:
put arrayName (Rand.Int (1, upper (arrayName))

Rand.Int returns a random integer value between two integers inputed, in this case 1 and upper (arrayName). upper returns the upper value of an array so in an array of 1 to 50 it would return 50
code:
var example : array 5..23
put upper (example) %outputs 23 to screen

now this random integer is then used as the subscript of th array so if it randomly chooses 5 then the the 5th element of the array will be outputted.[/code]
Bored




PostPosted: Tue Jun 13, 2006 9:45 pm   Post subject: (No subject)

Oh dang, you beat me to it, I need to learn to type and stop chiken pecking.
meidan




PostPosted: Tue Jun 13, 2006 9:48 pm   Post subject: (No subject)

Cervantes wrote:
code:

% lines is an array from 1 to 5 of string
put lines (Rand.Int (1, 5))


thank you sooo much! Smile

i used different array command but this still worked. I really appriciate everyone who helped me here thx ppl
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  [ 11 Posts ]
Jump to:   


Style:  
Search: