Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Rand.Int
Author Message
Azzy

Posted: Mon Nov 03, 2003 6:20 pm   Post subject: Rand.Int

how can you make it so it can only use one number one time?

Mazer

Posted: Mon Nov 03, 2003 6:41 pm   Post subject: (No subject)

could you explain what you mean?
poly

Posted: Mon Nov 03, 2003 6:46 pm   Post subject: (No subject)

I think he means, have say a randint randomizing from 1 to 6 and only having 1 show up once, 2 show up once etc.
Azzy

Posted: Mon Nov 03, 2003 6:50 pm   Post subject: (No subject)

exactly like poly said.like for the one i'm doing i have 52 but each one can only show up once.so there can only be 1 of the 35 variable in the array.
poly

Posted: Mon Nov 03, 2003 6:54 pm   Post subject: (No subject)

ok so have the randint and have an array keeping track of the numbers and have either an array or variable making sure no numbers are the same. I understand what you mean and how to do it, just I am shitty at explaining things just like above!
Tony

Posted: Mon Nov 03, 2003 8:10 pm   Post subject: (No subject)

What you should do is to have an array 1 to w/e largest number you want. Then you use Rand.Int to randombly pick an index of the number. It will be your first result. Then take that array element out and shift the array so that there're no gaps.

So each time, you pick a random number from 1 to n-try.
Tony's programming blog. DWITE - a programming contest.
nis

Posted: Thu Nov 06, 2003 6:56 pm   Post subject: (No subject)

Like tony said create an array
but create an array 1 to the highest num of boolean

and put the randint inside a loop
when a random number is choosen go to the array of that number and set it to true. and then exit the loop. If the array is allready true then have it pick another number

var Array : array 1 .. 6 of boolean init(false,false,false,false,false,false)
var num : int
loop
randint(num,1,6)
if not (Array(num)) then
Array(num) = true
exit
end if
end loop
%Then do what ever you have to do
Tony

Posted: Thu Nov 06, 2003 8:02 pm   Post subject: (No subject)

you got it all wrong. You suppost to generate a number number just once, not untill you pick one that wasn't used. What if it's a 1000 numbers to be generated? You'll have a 1/1000 chance of getting that last number and it will take you a LONG while.

here's my code:
 code: var randN:int %just to hold the number var numbers:array 1..10 of int for i:1..10 numbers(i):=i end for for decreasing i:10..1 randN := Rand.Int(1,i) put numbers(randN)     if randN not=i then     for a:randN+1..i     numbers(a-1):=numbers(a)     end for     end if end for
Tony's programming blog. DWITE - a programming contest.

nis

Posted: Fri Nov 07, 2003 12:00 am   Post subject: (No subject)

damn

That is good code

Showing me up like that
Tony

Posted: Fri Nov 07, 2003 10:12 am   Post subject: (No subject)

heh sorry nis
Tony's programming blog. DWITE - a programming contest.
Azzy

Posted: Fri Nov 07, 2003 9:10 pm   Post subject: (No subject)

There was a problem with the code.That is for an int array but I need that code for a string array.Is there any way you can change that?
Tony

Posted: Fri Nov 07, 2003 9:28 pm   Post subject: (No subject)