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

Username:   Password: 
 RegisterRegister   
 HELP!!! Random numbers and an array
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
python_4




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




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

such as this Random List Generator, check it out - it's just what you're looking for
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
McKenzie




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




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




PostPosted: Mon Jan 12, 2004 9:52 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
McKenzie




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




PostPosted: Mon Jan 12, 2004 11:48 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
McKenzie




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




PostPosted: Tue Jan 13, 2004 4:07 pm   Post subject: (No subject)

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



tony list 2.t
 Description:

Download
 Filename:  tony list 2.t
 Filesize:  741 Bytes
 Downloaded:  375 Time(s)

Cervantes




PostPosted: Tue Jan 13, 2004 4:14 pm   Post subject: (No subject)

go Mckenzie!! no.. go tony!!

*gives up and does the wave*
Tony




PostPosted: Tue Jan 13, 2004 4:18 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Cervantes




PostPosted: Tue Jan 13, 2004 4:21 pm   Post subject: (No subject)

*points at tony* BLASPHEMY!!!!


go Mckenzie!! go mckenzie!
McKenzie




PostPosted: Tue Jan 13, 2004 4:41 pm   Post subject: (No 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)
Tony




PostPosted: Tue Jan 13, 2004 6:30 pm   Post subject: (No subject)

haha Laughing Java or C++ ofcourse 8)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
python_4




PostPosted: Tue Jan 13, 2004 7:07 pm   Post subject: bitmaps

Bitmaps
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 2  [ 21 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: