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

Username:   Password: 
 RegisterRegister   
 Rand.Int
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Azzy




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Mazer




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

could you explain what you mean?
poly




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
nis




PostPosted: 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




PostPosted: Thu Nov 06, 2003 8:02 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
nis




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

damn


That is good code

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

Showing me up like that Crying or Very sad
Tony




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

heh Laughing sorry nis
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Azzy




PostPosted: 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




PostPosted: Fri Nov 07, 2003 9:28 pm   Post subject: (No 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
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  [ 12 Posts ]
Jump to:   


Style:  
Search: