
-----------------------------------
python_4
Mon Jan 12, 2004 6:29 pm

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.

-----------------------------------
Tony
Mon Jan 12, 2004 6:33 pm


-----------------------------------
seems you're new, so I'll show you around a bit 8) There's this very interesting place on this forum called [url=http://www.compsci.ca/v2/viewforum.php?f=48][Turing Source Code] The interesting thing is that it has a bunch of writen code that could have just what you're looking for :wink:

such as this [url=http://www.compsci.ca/v2/viewtopic.php?t=2184]Random List Generator, check it out - it's just what you're looking for

-----------------------------------
McKenzie
Mon Jan 12, 2004 6:36 pm


-----------------------------------
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
Mon Jan 12, 2004 6:42 pm

Thank you
-----------------------------------
Thank you very much, that was exactly what i was looking for. I really appreciate for your help and time.

-----------------------------------
Tony
Mon Jan 12, 2004 9:52 pm


-----------------------------------
randomly pick a number that has not been picked

 :cry: please stop curropting young programmers with false ways :lol:

I mean you're a teacher... shouldn't you like promote efficiency and correct programming approaches or something :think:

-----------------------------------
McKenzie
Mon Jan 12, 2004 11:16 pm


-----------------------------------
yer kiddin right? Run your program and check the number of milliseconds, then run mine. Hmmm ... 190 vs 14 on my computer
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
Mon Jan 12, 2004 11:48 pm


-----------------------------------
:? 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 :wink:

-----------------------------------
McKenzie
Tue Jan 13, 2004 9:50 am


-----------------------------------
Oh you mean like: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)

-----------------------------------
McKenzie
Tue Jan 13, 2004 4:07 pm


-----------------------------------
Hey Tony, 
I tried to time yours like mine, it kinda freezes.

-----------------------------------
Cervantes
Tue Jan 13, 2004 4:14 pm


-----------------------------------
go Mckenzie!!   no.. go tony!!

*gives up and does the wave*

-----------------------------------
Tony
Tue Jan 13, 2004 4:18 pm


-----------------------------------
hahaha :lol: 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 :wink:

-----------------------------------
Cervantes
Tue Jan 13, 2004 4:21 pm


-----------------------------------
*points at tony*  BLASPHEMY!!!!


go Mckenzie!! go mckenzie!

-----------------------------------
McKenzie
Tue Jan 13, 2004 4:41 pm


-----------------------------------
Well, which one would you prefer?
QBASIC, Fortran, Pascal, Javascript, C, C++, Miranda, Prolog, simscript, Smalltalk, 8088 ASM, Visual Basic, GPSS, COBOL???

P.S. please don't pick COBOL (ouch)

-----------------------------------
Tony
Tue Jan 13, 2004 6:30 pm


-----------------------------------
haha :lol: Java or C++ ofcourse 8)

-----------------------------------
python_4
Tue Jan 13, 2004 7:07 pm

bitmaps
-----------------------------------
Bitmaps

-----------------------------------
Maverick
Tue Jan 13, 2004 7:23 pm


-----------------------------------
Hmm I get it? :doh:  :wall:

-----------------------------------
Andy
Tue Jan 13, 2004 8:06 pm


-----------------------------------
lol mickey doesnt know java

-----------------------------------
McKenzie
Tue Jan 13, 2004 8:56 pm


-----------------------------------
:D Well I don't know Java yet, I do plan on teaching it next year for the gr12 class. But seriously the language won't effect the outcome. Consider the following:

for a 100000 element array
# filled       | approx num tries per   |  sub-total
-------------------------------------------
0-50000        |   1.4                  |  70001
50001-75000    |   2.8                  |  70000
75001-95000    |   8.9                  | 178000
95000-99000    |   45                   | 180045
99001-99900    |   255                  | 229500
99900-99999    |   5110                 | 511000
                           Total        |1238546
for 100000 elements that makes it of order n. 12.3 n is of order n.

-----------------------------------
Dauntless
Tue Jan 13, 2004 9:01 pm


-----------------------------------
Help!! Random numbers and an array!

or

How helping a new programmer can turn into a battle of the titans.

-----------------------------------
Tony
Tue Jan 13, 2004 9:05 pm


-----------------------------------
:lol:

bah... thats what I get for theoretical programming :roll:

-----------------------------------
Tony
Sat Jan 17, 2004 8:49 pm


-----------------------------------
ha, I knew I was right :D

I was looking at my code and though - "what the hell was that guy thinking?"

then I realized it was my code and fixed it. Edited version of algorythm is found [url=http://www.compsci.ca/v2/viewtopic.php?p=18510]in the same place

the problem was how I was shifting my array. I used to throw out one element and moved everything to the left from that point on. But then (now) I figured that there was no point in keeping the remainding numbers in order so I just moved the last element in place of one to be deleted :lol:

the result? this cut down the execution speed to just 2% of previous algorythm (tested at size=1000) :lol:

McKenzie - would you like to run some more tests now :wink: I think I have a better chance.
