Author |
Message |
Shamikh
|
Posted: 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. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: 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() |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Shamikh
|
Posted: 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 |
|
|
|
|
 |
Shamikh
|
Posted: 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 |
|
|
|
|
 |
Tony

|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Shamikh
|
Posted: 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. |
|
|
|
|
 |
Shamikh
|
Posted: 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)) |
|
|
|
|
 |
Tony

|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Shamikh
|
Posted: 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? |
|
|
|
|
 |
Tony

|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Shamikh
|
Posted: Mon Jun 02, 2008 8:23 pm Post subject: RE:Randomly Choosing 10 out of 40 |
|
|
Still working on it, Remembr im Grade 9 |
|
|
|
|
 |
Shamikh
|
Posted: Mon Jun 02, 2008 8:26 pm Post subject: RE:Randomly Choosing 10 out of 40 |
|
|
It keeps saying lol has been previoulsy decalred |
|
|
|
|
 |
Shamikh
|
Posted: Mon Jun 02, 2008 8:29 pm Post subject: RE:Randomly Choosing 10 out of 40 |
|
|
Someone Give me a hint.
Its Soooo Frustrating! |
|
|
|
|
 |
richcash
|
Posted: 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. |
|
|
|
|
 |
Tony

|
Posted: 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 |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
|