Computer Science Canada

HELP!!! Random numbers and an array

Author:  python_4 [ 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.

Author:  Tony [ Mon Jan 12, 2004 6:33 pm ]
Post 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 Wink

such as this Random List Generator, check it out - it's just what you're looking for

Author:  McKenzie [ Mon Jan 12, 2004 6:36 pm ]
Post 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.

Author:  python_4 [ 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.

Author:  Tony [ Mon Jan 12, 2004 9:52 pm ]
Post subject: 

McKenzie wrote:
randomly pick a number that has not been picked


Crying or Very sad please stop curropting young programmers with false ways Laughing

I mean you're a teacher... shouldn't you like promote efficiency and correct programming approaches or something Thinking

Author:  McKenzie [ Mon Jan 12, 2004 11:16 pm ]
Post 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

Author:  Tony [ Mon Jan 12, 2004 11:48 pm ]
Post subject: 

Confused 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

Author:  McKenzie [ Tue Jan 13, 2004 9:50 am ]
Post 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)

Author:  McKenzie [ Tue Jan 13, 2004 4:07 pm ]
Post subject: 

Hey Tony,
I tried to time yours like mine, it kinda freezes.

Author:  Cervantes [ Tue Jan 13, 2004 4:14 pm ]
Post subject: 

go Mckenzie!! no.. go tony!!

*gives up and does the wave*

Author:  Tony [ Tue Jan 13, 2004 4:18 pm ]
Post subject: 

hahaha Laughing well shit Confused 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 Sad

I think we need to continue this discussion in another language that doesn't suck as much as turing Wink

Author:  Cervantes [ Tue Jan 13, 2004 4:21 pm ]
Post subject: 

*points at tony* BLASPHEMY!!!!


go Mckenzie!! go mckenzie!

Author:  McKenzie [ Tue Jan 13, 2004 4:41 pm ]
Post subject: 

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)

Author:  Tony [ Tue Jan 13, 2004 6:30 pm ]
Post subject: 

haha Laughing Java or C++ ofcourse 8)

Author:  python_4 [ Tue Jan 13, 2004 7:07 pm ]
Post subject:  bitmaps

Bitmaps

Author:  Maverick [ Tue Jan 13, 2004 7:23 pm ]
Post subject: 

Hmm I get it? Doh! Hit Wall

Author:  Andy [ Tue Jan 13, 2004 8:06 pm ]
Post subject: 

lol mickey doesnt know java

Author:  McKenzie [ Tue Jan 13, 2004 8:56 pm ]
Post subject: 

Very Happy 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:
code:

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.

Author:  Dauntless [ Tue Jan 13, 2004 9:01 pm ]
Post subject: 

Help!! Random numbers and an array!

or

How helping a new programmer can turn into a battle of the titans.

Author:  Tony [ Tue Jan 13, 2004 9:05 pm ]
Post subject: 

Laughing

bah... thats what I get for theoretical programming Rolling Eyes

Author:  Tony [ Sat Jan 17, 2004 8:49 pm ]
Post subject: 

ha, I knew I was right Very Happy

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 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 Laughing

the result? this cut down the execution speed to just 2% of previous algorythm (tested at size=1000) Laughing

McKenzie - would you like to run some more tests now Wink I think I have a better chance.


: