Computer Science Canada

Randomly Choosing 10 out of 40

Author:  Shamikh [ Mon Jun 02, 2008 7:14 pm ]
Post subject:  Randomly Choosing 10 out of 40

I need to be able to choose randomly 10 questions out of a possible 40:

code:

lol (1) := question1
lol (2) := question2
lol (3) := question3
lol (4) := question4
lol (5) := question5
lol (6) := question6
lol (7) := question7
lol (8) := question8
lol (9) := question9
lol (10) := question10
lol (11) := question11
lol (12) := question12
lol (13) := question13
lol (14) := question14
lol (15) := question15
lol (16) := question16
lol (17) := question17
lol (18) := question18
lol (19) := question19
lol (20) := question20
lol (21) := question21
lol (22) := question22
lol (23) := question23
lol (24) := question24
lol (26) := question26
lol (27) := question27
lol (28) := question28
lol (29) := question29
lol (30) := question30
lol (31) := question31
lol (32) := question32
lol (33) := question33
lol (34) := question34
lol (35) := question35
lol (36) := question36
lol (37) := question37
lol (38) := question38
lol (39) := question39
lol (40) := question40


lol is my array and the question1 etc.. are procedures. The only way i would do it is to somehow assign every question an int value and then randint, but i dont
know how. Some help plz.
code:

Author:  Tony [ Mon Jun 02, 2008 7:20 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

every question already has an int value -- the index of the array.

Also, randint is deprecated, use Rand.Int()

Author:  Shamikh [ Mon Jun 02, 2008 7:22 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

What do u mean the index of the array.
Sorry im so bad, im in grade 9 Compsci

Author:  Shamikh [ Mon Jun 02, 2008 7:29 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

understand the index but how do iput that into my randint equation?
p.s Rand.Int isnt working for me

Author:  Tony [ Mon Jun 02, 2008 7:44 pm ]
Post subject:  Re: RE:Randomly Choosing 10 out of 40

Shamikh @ Mon Jun 02, 2008 7:29 pm wrote:
p.s Rand.Int isnt working for me

You're not doing it right.

Turing:

lol(Rand.Int(1,40))


Of course your next problem would be to make sure that you don't pick the same question twice. The usual approach is to have another array of indexes of elements that were not yet selected, and pick randomly from _that_ array.

Author:  Shamikh [ Mon Jun 02, 2008 7:47 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

So make another array from 1 to 40 then do the same thing with that? I dont quite understand.

Author:  Shamikh [ Mon Jun 02, 2008 7:48 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

So like this:
olo(lol(Rand.Int(1,40))

Author:  Tony [ Mon Jun 02, 2008 7:57 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

no, like over here

Author:  Shamikh [ Mon Jun 02, 2008 8:19 pm ]
Post subject:  Re: Randomly Choosing 10 out of 40

So i would make the following changes to this code to fit my program:

code:
var size:int :=10
var randN:int %just to hold the number
var numbers:array 1..size of int
for i:1..size
numbers(i):=i
end for

for decreasing i:size ..1
randN := Rand.Int(1,i)
put numbers(randN)

    numbers(randN):=numbers(i)

end for


Where would my lol go?

Author:  Tony [ Mon Jun 02, 2008 8:21 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

Once you figure out how that code snippet works, it should be rather obvious.

Author:  Shamikh [ Mon Jun 02, 2008 8:23 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

Still working on it, Remembr im Grade 9

Author:  Shamikh [ Mon Jun 02, 2008 8:26 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

It keeps saying lol has been previoulsy decalred

Author:  Shamikh [ Mon Jun 02, 2008 8:29 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

Someone Give me a hint.
Its Soooo Frustrating!

Author:  richcash [ Mon Jun 02, 2008 8:41 pm ]
Post subject:  Re: Randomly Choosing 10 out of 40

A hint? Well, lol is an array and I only see one array in that code snippet.

Author:  Tony [ Mon Jun 02, 2008 8:48 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

forget about lol for a moment. Start by explaining how that code gets to generate a random list.

protip: there's an explanation in the thread where you copied that code from.

Then figure out how to take numbers from that random list, one at a time.

Then we are back to
code:
lol(index)

Author:  Shamikh [ Mon Jun 02, 2008 8:57 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

THATS what im trying to do. Take one number from that list at a time and use that particular question

Author:  Shamikh [ Mon Jun 02, 2008 9:01 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

so "size" is the number of numbers it creates,
"number" is the name of the array and it says from 1 to whatever your size is. and
"numbers(i):=i' tells it how many pockets in the array. am i right?

Author:  Shamikh [ Mon Jun 02, 2008 9:06 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

"randN" is the random number and "number" is the number of outputted integers.

numbers(randN):=numbers(i) makes sure no numbers are exactly the same.

but i still dont understand how to pull one number from that random generation,

Author:  Tony [ Mon Jun 02, 2008 9:07 pm ]
Post subject:  Re: RE:Randomly Choosing 10 out of 40

Shamikh @ Mon Jun 02, 2008 9:01 pm wrote:
"numbers(i):=i' tells it how many pockets in the array. am i right?

No.

If you need to review how arrays work, you can find the tutorials through the Turing Walkthrough

Author:  Tony [ Mon Jun 02, 2008 9:09 pm ]
Post subject:  Re: RE:Randomly Choosing 10 out of 40

Shamikh @ Mon Jun 02, 2008 9:06 pm wrote:
numbers(randN):=numbers(i) makes sure no numbers are exactly the same.

That is also wrong.

Author:  Shamikh [ Mon Jun 02, 2008 9:12 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

could you be so kind as to give a tiny hint

i kno i have to have:

lol (part of that code)

just looked at the arrays tutorial.

Author:  Tony [ Mon Jun 02, 2008 9:19 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

how does that code know which number to print to screen?

Also -- I am fully expecting you to cite the source of this piece of code, if you'll end up using that in your assignment.

Author:  Shamikh [ Mon Jun 02, 2008 9:25 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

from this piece of code

for decreasing i:size ..1
randN := Rand.Int(1,i)
put numbers(randN)

P.s--Also this is not for an assignment, rather for something else i am working on.

Author:  Tony [ Mon Jun 02, 2008 9:28 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

well then in the similar matter you can get that random number, and instead of puting it to screen - call your procedure

Author:  Shamikh [ Mon Jun 02, 2008 9:37 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

What do u mean. call my procedure.
So instead of Put
i do lol(Rand.Int(1,40))?

Author:  Tony [ Mon Jun 02, 2008 9:47 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

that would be a good place to start.. yes.

Author:  Shamikh [ Mon Jun 02, 2008 10:13 pm ]
Post subject:  RE:Randomly Choosing 10 out of 40

Thanks very much, for being patient with me. I could telli was getting on your nerves. I dont blame ya, If i was you so would i. Thanks again man.


: