Posted: Mon Jan 12, 2004 6:29 pm Post subject: HELP!!! Random numbers and an array
Hey guys, I need help with my project here. The problem is that I cannot make a program that will randomly select 9 numbers out of 1..20 and not select the selected numbers again. That is, i want help in creating a program which will select 9 DIFFERENT numbers from 1.. 20 and store it in different variables or an array, so that I can later use the array to call upon text files. That is, each number between 1..20 represents a text file. Please take into consideration that I must be able to use the numbers to call upon TEXT files. I would really really really appreciate if someone would help me out.
Sponsor Sponsor
Tony
Posted: Mon Jan 12, 2004 6:33 pm Post subject: (No subject)
seems you're new, so I'll show you around a bit 8) There's this very interesting place on this forum called [Turing Source Code] The interesting thing is that it has a bunch of writen code that could have just what you're looking for
such as this Random List Generator, check it out - it's just what you're looking for
Posted: Mon Jan 12, 2004 6:36 pm Post subject: (No subject)
So it sounds like you are making some trivia game and you want random catagories. You will need two arrays One to store your random picks and one to keep track of the spots you have used. Run a for loop from 1 to 9. Inside the for you must ensure that you randomly pick a unique number. What you need is a loop. You will keep looping until you randomly pick a number that has not been picked, then record that it has been picked.
python_4
Posted: Mon Jan 12, 2004 6:42 pm Post subject: Thank you
Thank you very much, that was exactly what i was looking for. I really appreciate for your help and time.
Tony
Posted: Mon Jan 12, 2004 9:52 pm Post subject: (No subject)
McKenzie wrote:
randomly pick a number that has not been picked
please stop curropting young programmers with false ways
I mean you're a teacher... shouldn't you like promote efficiency and correct programming approaches or something
Posted: Mon Jan 12, 2004 11:16 pm Post subject: (No subject)
yer kiddin right? Run your program and check the number of milliseconds, then run mine. Hmmm ... 190 vs 14 on my computer
code:
var now : int
var randN : int
var numbers : array 1 .. 10 of int
for i : 1 .. 10
numbers (i) := 1
end for
for i : 1 .. 10
loop
randN := Rand.Int (1, 10)
exit when numbers (randN) = 1
end loop
put randN
numbers (randN) := 0
end for
clock (now)
put now
Tony
Posted: Mon Jan 12, 2004 11:48 pm Post subject: (No subject)
try cloacking the programs with larger numbers. Seing as the running time is exponensial for yours and linear for mine... There got to be that magic number at which point my program gets more efficient
Posted: Tue Jan 13, 2004 9:50 am Post subject: (No subject)
Oh you mean like:
code:
var now : int
var start : int
var randN : int
var n:int
var trialTimes : array 1 .. 5 of int
var arrSize : array 1 .. 5 of int := init (10, 100, 1000, 10000, 100000)
for trial : 1 .. 5
clock (start)
n:=arrSize(trial)
var numbers : array 1 .. n of int
for i : 1 .. n
numbers (i) := 1
end for
for i : 1 .. n
loop
randN := Rand.Int (1, n)
exit when numbers (randN) = 1
end loop
numbers (randN) := 0
end for
clock (now)
trialTimes (trial) := now - start
put trialTimes(trial)
end for
put "Trial":10,"size":10,"time":10,"time/n":10
for i : 1 .. 5
put i : 10,arrSize(i):10, trialTimes (i) : 5,trialTimes (i)/arrSize(i):10:1
end for
Run it, it's O(n)
Sponsor Sponsor
McKenzie
Posted: Tue Jan 13, 2004 4:07 pm Post subject: (No subject)
Hey Tony,
I tried to time yours like mine, it kinda freezes.
Posted: Tue Jan 13, 2004 4:14 pm Post subject: (No subject)
go Mckenzie!! no.. go tony!!
*gives up and does the wave*
Tony
Posted: Tue Jan 13, 2004 4:18 pm Post subject: (No subject)
hahaha well shit In theory it is suppost to be faster since it's not exponensial algorythm... but I suppose variable declaration and array rearrangement slows it down enough
I think we need to continue this discussion in another language that doesn't suck as much as turing