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

Username:   Password: 
 RegisterRegister   
 turing hep--- randomizing
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
coolguy




PostPosted: Mon Jun 09, 2008 3:03 pm   Post subject: turing hep--- randomizing

i want to make a program which shows # frm 0-9 in randomize order...it shuldn't duplicate any number.......need help
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Jun 09, 2008 4:16 pm   Post subject: RE:turing hep--- randomizing

I believe this should have been posted in the 'help' section, but heck, I'll help anyway, I'm in a good mood.

To make a random number, us the Rand.Int command. This generates a number based on a complicated equation based on the current time (as a computer cannot produce anything with true randomness). The command is the following:

code:

variable := Rand.Int (smallestnumber, biggestnumber)
.

Now I'm going to assume you already know that, so I won't elaborate.

One way to do this is to create an extra variable as an array and set all of it to a number outside the scope of the random number (-1 or 10, in this case). Once you make a random number, add that into the array. Every time you create a new number, check through the array (in a for loop) for any occurence of that number. If that number has been used before, create a different number.
coolguy




PostPosted: Mon Jun 09, 2008 4:46 pm   Post subject: Re: turing hep--- randomizing

var list : array 1 .. 10 of int
randomize
var random_number : int
for i : 1 .. 10
randint (random_number, 0, 9)

put random_number : 3 ..
end for



heres my code...wat do i change in this...
Insectoid




PostPosted: Mon Jun 09, 2008 5:18 pm   Post subject: RE:turing hep--- randomizing

First, don't use randint, use Rand.Int. It's like the mature couson of randint. It's beneficial because it does not require a variable to work.

What you have so far is good. You just have to check to see if that nmber has been used before.

Use a for loop to initialize the array

code:

for x: 1..10
     list (x) := -1
end for


Then, once you have created a random number, check if it's been used before.

code:

var numgood : boolean := true
for x: 1..10
     if random_number = list (x) then
          numgood := false     
    end if
end for


This is VERY inefficient and you'll only want to use it as a base from which to build.
Saad




PostPosted: Mon Jun 09, 2008 5:40 pm   Post subject: Re: turing hep--- randomizing

Another way that is more simple, create an array of numbers from 1->10. Now in a loop, shuffle the numbers by swapping the numbers at 2 indexes. (Use Rand.Int to pick a random index for the two locations and swap them)
riveryu




PostPosted: Mon Jun 09, 2008 11:19 pm   Post subject: Re: turing hep--- randomizing

@Saad

Do you mean using xor to switch the values? Can you give more of a hint?
like
Turing:

var a:int:=1
var b:int:=2
a:= a xor b
b:= a xor b
a:=a xor b
put a
put b


Can't Rand.Int repeat the same value when choosing 2 indexes, ex. you swap num(1) with (1) randomly.
Tony




PostPosted: Tue Jun 10, 2008 12:51 am   Post subject: RE:turing hep--- randomizing

well ideally the number stored in index 1 should have the same chance to appear in index 1 (so not be moved) as it has to appear in any other particular slot.

There are more efficient (and accurate) ways of generating random lists, then shuffling an array. Well, efficient in terms of CPU, it takes up twice as much memory. Though considering the size of the problem (10 elements? that's negligible)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Rwed




PostPosted: Sat Jan 08, 2011 5:44 pm   Post subject: RE:turing hep--- randomizing

would you know how to randomize songs then?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sat Jan 08, 2011 5:51 pm   Post subject: RE:turing hep--- randomizing

You have a list of songs, and you randomly pick a position in that list.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
2goto1




PostPosted: Sat Jan 08, 2011 6:02 pm   Post subject: RE:turing hep--- randomizing

hit "shuffle" in your go-to media player and you're all set
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  [ 10 Posts ]
Jump to:   


Style:  
Search: