Computer Science Canada

Rand.Int

Author:  Azzy [ 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?

Author:  Mazer [ Mon Nov 03, 2003 6:41 pm ]
Post subject: 

could you explain what you mean?

Author:  poly [ Mon Nov 03, 2003 6:46 pm ]
Post 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.

Author:  Azzy [ Mon Nov 03, 2003 6:50 pm ]
Post 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.

Author:  poly [ Mon Nov 03, 2003 6:54 pm ]
Post 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!

Author:  Tony [ Mon Nov 03, 2003 8:10 pm ]
Post 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.

Author:  nis [ Thu Nov 06, 2003 6:56 pm ]
Post 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

Author:  Tony [ Thu Nov 06, 2003 8:02 pm ]
Post subject: 

bad nis Rolling Eyes tsk tsk tsk Laughing

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

Author:  nis [ Fri Nov 07, 2003 12:00 am ]
Post subject: 

damn


That is good code

Twisted Evil *cough* Watch your back *cough* Rolling Eyes

Showing me up like that Crying or Very sad

Author:  Tony [ Fri Nov 07, 2003 10:12 am ]
Post subject: 

heh Laughing sorry nis

Author:  Azzy [ Fri Nov 07, 2003 9:10 pm ]
Post 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?

Author:  Tony [ Fri Nov 07, 2003 9:28 pm ]
Post subject: 

Confused You asked for numbers.
Quote:
use one number one time


ether way. You get a list of numbers - use them as your array indexes to pick stings out.


: