By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,234 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,234 IT Pros & Developers. It's quick & easy.

randomising for a quiz

P: n/a
Dear All

I am making a quiz with a four-option-multiple-choice scenario.

When you enter the quiz, you provide one answer and three wrong answers.

Then the contestant opens the quiz and I trying to present them the four
answers on the page in a randomized way (i.e. you have no idea which one
comes first).

I know the rnd command, but how do you do rnd without replacement?

TIA

- Nicolaas

Nov 13 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
This is math not a computer group.
"WindAndWaves" <ac****@ngaru.com> wrote in message
news:TH********************@news.xtra.co.nz...
Dear All

I am making a quiz with a four-option-multiple-choice scenario.

When you enter the quiz, you provide one answer and three wrong answers.

Then the contestant opens the quiz and I trying to present them the four
answers on the page in a randomized way (i.e. you have no idea which one
comes first).

I know the rnd command, but how do you do rnd without replacement?

TIA

- Nicolaas


Nov 13 '05 #2

P: n/a
On Fri, 27 Aug 2004 15:14:52 +1200, "WindAndWaves" <ac****@ngaru.com>
wrote:
Dear All

I am making a quiz with a four-option-multiple-choice scenario.

When you enter the quiz, you provide one answer and three wrong answers.

Then the contestant opens the quiz and I trying to present them the four
answers on the page in a randomized way (i.e. you have no idea which one
comes first).

I know the rnd command, but how do you do rnd without replacement?

TIA

- Nicolaas


Set up an array of 4 elements.
Randomly choose one.
Remove it from the list.
Randomly choose one of the 3.
And so on.
Nov 13 '05 #3

P: n/a
Dear Michael

I had a go at this and this is what I came up with (slightly different):

Private Sub Form_Current()
'randomises the anwsers
Const ProEro = 12: 'on error GoTo ERR
'---
Dim Ctl As Control
Dim C(3) As Byte
Dim I As Integer
Dim J As Integer
'---assign numbers
For I = 0 To 3
TryAgain:
C(I) = Int(Rnd * 5)
If C(I) = 0 Or C(I) = 5 Then GoTo TryAgain
If I = 0 Then GoTo looper
'--- check if it exists already and try again if this is so
For J = (I - 1) To 0 Step -1
If C(I) = C(J) Then GoTo TryAgain
Next J
looper:
Next I
'---place data
For I = 0 To 3
Set Ctl = Me.Controls("O" & C(I))
Ctl.ControlSource = "=[QA" & I & "]"
Ctl.Tag = I
'---reset tickbox
Me.Controls("C" & I + 1) = False
Next I
xit:
Exit Sub
ERR:
Call FerrorLog(ERR.Number, 0, ProEro + Modero): Resume Next
End Sub
"Michael Gray" <fl****@newsguy.spam.com> wrote in message
news:dh********************************@4ax.com...
On Fri, 27 Aug 2004 15:14:52 +1200, "WindAndWaves" <ac****@ngaru.com>
wrote:
Dear All

I am making a quiz with a four-option-multiple-choice scenario.

When you enter the quiz, you provide one answer and three wrong answers.

Then the contestant opens the quiz and I trying to present them the four
answers on the page in a randomized way (i.e. you have no idea which one
comes first).

I know the rnd command, but how do you do rnd without replacement?

TIA

- Nicolaas


Set up an array of 4 elements.
Randomly choose one.
Remove it from the list.
Randomly choose one of the 3.
And so on.

Nov 13 '05 #4

P: n/a
On Sat, 28 Aug 2004 00:21:41 +1200, "WindAndWaves" <ac****@ngaru.com>
wrote:
Dear Michael

I had a go at this and this is what I came up with (slightly different):

:

If all you are trying to achieve is to get it to work, by far and away
the most efficient method (but no the most elegant perhaps), is to
pre-populate an array with the 24 possible permutations (of the four
questions), and choose one randomly from this list.
This is much more efficient than any other way.

So you would do something like this the first time the program runs:

Dim Permutation(24) As String
Permutation(0) = "1234"
Permutation(1) = "1243"
Permutation(2) = "1324"
Permutation(3) = "1342"
Permutation(4) = "1432"
Permutation(5) = "1423"
Permutation(6) = "2134"
Permutation(7) = "2143"
Permutation(8) = "2314"
Permutation(9) = "2341"
Permutation(10) = "2431"
Permutation(11) = "2413"
Permutation(12) = "3124"
Permutation(13) = "3142"
Permutation(14) = "3214"
Permutation(15) = "3241"
Permutation(16) = "3412"
Permutation(17) = "3421"
Permutation(18) = "4123"
Permutation(19) = "4132"
Permutation(20) = "4213"
Permutation(21) = "4231"
Permutation(22) = "4312"
Permutation(23) = "4321"

(I have used strings here for ease of demonstration only, and assume
an array base of zero [Option Base 0])

Then your subroutine would select one of these 24 elements at random,
and apply the order chosen.
I will leave it to you work out the mechanics...
Nov 13 '05 #5

P: n/a
That is a nice solution Michael, thank you for that idea.
"Michael Gray" <fl****@newsguy.spam.com> wrote in message
news:d4********************************@4ax.com...
On Sat, 28 Aug 2004 00:21:41 +1200, "WindAndWaves" <ac****@ngaru.com>
wrote:
Dear Michael

I had a go at this and this is what I came up with (slightly different):

:

If all you are trying to achieve is to get it to work, by far and away
the most efficient method (but no the most elegant perhaps), is to
pre-populate an array with the 24 possible permutations (of the four
questions), and choose one randomly from this list.
This is much more efficient than any other way.

So you would do something like this the first time the program runs:

Dim Permutation(24) As String
Permutation(0) = "1234"
Permutation(1) = "1243"
Permutation(2) = "1324"
Permutation(3) = "1342"
Permutation(4) = "1432"
Permutation(5) = "1423"
Permutation(6) = "2134"
Permutation(7) = "2143"
Permutation(8) = "2314"
Permutation(9) = "2341"
Permutation(10) = "2431"
Permutation(11) = "2413"
Permutation(12) = "3124"
Permutation(13) = "3142"
Permutation(14) = "3214"
Permutation(15) = "3241"
Permutation(16) = "3412"
Permutation(17) = "3421"
Permutation(18) = "4123"
Permutation(19) = "4132"
Permutation(20) = "4213"
Permutation(21) = "4231"
Permutation(22) = "4312"
Permutation(23) = "4321"

(I have used strings here for ease of demonstration only, and assume
an array base of zero [Option Base 0])

Then your subroutine would select one of these 24 elements at random,
and apply the order chosen.
I will leave it to you work out the mechanics...

Nov 13 '05 #6

P: n/a
On Sun, 29 Aug 2004 08:46:02 +1200, "WindAndWaves" <ac****@ngaru.com>
wrote:
That is a nice solution Michael, thank you for that idea.


Actually it's not my idea.
It dates from ancient times.
Logarithm tables are a good example, as are "Ready Reckoners" which
were essential in the days of Pounds, Shillings & Pence.
Often pre-populated tables are the most effective way to go with a lot
of problems, although it's surprising how many programmers shy away
from them, or don't even think of them.
They are particularly handy in programs that perform intensive
trigonometry. (Even if a F.P. processor is available)
A table of sin(x) covering an 8th of a circle to the required
precision is all that one needs. The rest of the circle can be
synthesised without having to call transcendental functions
repeatedly.
It's used a lot by people who program microprocessors in machine code,
who *do* tend to think this way.

Michael G.
Nov 13 '05 #7

P: n/a
Michael Gray <fl****@newsguy.spam.com> wrote in message news:<d4********************************@4ax.com>. ..
On Sat, 28 Aug 2004 00:21:41 +1200, "WindAndWaves" <ac****@ngaru.com>
wrote:
Dear Michael

I had a go at this and this is what I came up with (slightly different):

:

If all you are trying to achieve is to get it to work, by far and away
the most efficient method (but no the most elegant perhaps), is to
pre-populate an array with the 24 possible permutations (of the four
questions), and choose one randomly from this list.
This is much more efficient than any other way.

So you would do something like this the first time the program runs:

Dim Permutation(24) As String
Permutation(0) = "1234"
Permutation(1) = "1243"
Permutation(2) = "1324"
Permutation(3) = "1342"
Permutation(4) = "1432"
Permutation(5) = "1423"
Permutation(6) = "2134"
Permutation(7) = "2143"
Permutation(8) = "2314"
Permutation(9) = "2341"
Permutation(10) = "2431"
Permutation(11) = "2413"
Permutation(12) = "3124"
Permutation(13) = "3142"
Permutation(14) = "3214"
Permutation(15) = "3241"
Permutation(16) = "3412"
Permutation(17) = "3421"
Permutation(18) = "4123"
Permutation(19) = "4132"
Permutation(20) = "4213"
Permutation(21) = "4231"
Permutation(22) = "4312"
Permutation(23) = "4321"

(I have used strings here for ease of demonstration only, and assume
an array base of zero [Option Base 0])

Then your subroutine would select one of these 24 elements at random,
and apply the order chosen.
I will leave it to you work out the mechanics...


or.. more simply and elegantly.. pick a random number from 1 to 24..

then u can sieve through the # to find out which permutation it is..

if it's in the first 6.. 1 is first
second 6 2 is first
etc.

within that group of six..
if it's in the first 2.. etc.

in the 2.. the order of the two will determine the order of the last two...
it's very simple..
Nov 13 '05 #8

P: n/a
On 15 Sep 2004 19:21:56 -0700, dw*****@yahoo.com (David Bandel) wrote:
Michael Gray <fl****@newsguy.spam.com> wrote in message news:<d4********************************@4ax.com>. ..
On Sat, 28 Aug 2004 00:21:41 +1200, "WindAndWaves" <ac****@ngaru.com>
wrote:
>Dear Michael
>
>I had a go at this and this is what I came up with (slightly different):
>

:

If all you are trying to achieve is to get it to work, by far and away
the most efficient method (but no the most elegant perhaps), is to
pre-populate an array with the 24 possible permutations (of the four
questions), and choose one randomly from this list.
This is much more efficient than any other way.

So you would do something like this the first time the program runs:

Dim Permutation(24) As String
Permutation(0) = "1234"
Permutation(1) = "1243"
Permutation(2) = "1324"
Permutation(3) = "1342"
Permutation(4) = "1432"
Permutation(5) = "1423"
Permutation(6) = "2134"
Permutation(7) = "2143"
Permutation(8) = "2314"
Permutation(9) = "2341"
Permutation(10) = "2431"
Permutation(11) = "2413"
Permutation(12) = "3124"
Permutation(13) = "3142"
Permutation(14) = "3214"
Permutation(15) = "3241"
Permutation(16) = "3412"
Permutation(17) = "3421"
Permutation(18) = "4123"
Permutation(19) = "4132"
Permutation(20) = "4213"
Permutation(21) = "4231"
Permutation(22) = "4312"
Permutation(23) = "4321"

(I have used strings here for ease of demonstration only, and assume
an array base of zero [Option Base 0])

Then your subroutine would select one of these 24 elements at random,
and apply the order chosen.
I will leave it to you work out the mechanics...


or.. more simply and elegantly.. pick a random number from 1 to 24..

then u can sieve through the # to find out which permutation it is..

if it's in the first 6.. 1 is first
second 6 2 is first
etc.

within that group of six..
if it's in the first 2.. etc.

in the 2.. the order of the two will determine the order of the last two...
it's very simple..


I agree.

But for time critical routines, there's no substitute for a
pre-populated table.
I don't implement it as above, of course, but as an array of arrays.
One random look-up gives you the order without having to go through
seiving ranges.
They are "Pre-seived" as it were.

But you are right: your way is more elegant,
but I dispute that it is "simpler" to actually implement.
Nov 13 '05 #9

P: n/a
Here's what i came up with:

I assume the table with the questions has the following format:

Id
Question
RightAnswer
WrongAnswer1
WrongAnswer2
WrongAnswer3

Now the following query will give you the answers to question 1 in
random order:

SELECT Answer, Correct
FROM
(SELECT RND() AS Position, RightAnswer AS Answer, True AS Correct FROM
Questions WHERE Id = 1
UNION ALL
SELECT RND() AS Position, WrongAnswer1, False AS Correct FROM
Questions WHERE Id = 1
UNION ALL
SELECT RND() AS Position, WrongAnswer2, False AS Correct FROM
Questions WHERE Id = 1
UNION ALL SELECT RND() AS Position, WrongAnswer3, False AS Correct
FROM Questions WHERE Id = 1)
ORDER BY Position;

I hardcoded question 1 in there to keep it simple, when you open this
in VBA you replace that with the Id of the question you want to ask.
It almost as lengthy as the other alternative, it just looks more
'pure' to me.

Regards,

GJ
Michael Gray <fl****@newsguy.spam.com> wrote in message news:<k8********************************@4ax.com>. ..
On 15 Sep 2004 19:21:56 -0700, dw*****@yahoo.com (David Bandel) wrote:
Michael Gray <fl****@newsguy.spam.com> wrote in message news:<d4********************************@4ax.com>. ..
On Sat, 28 Aug 2004 00:21:41 +1200, "WindAndWaves" <ac****@ngaru.com>
wrote:

>Dear Michael
>
>I had a go at this and this is what I came up with (slightly different):
>
:

If all you are trying to achieve is to get it to work, by far and away
the most efficient method (but no the most elegant perhaps), is to
pre-populate an array with the 24 possible permutations (of the four
questions), and choose one randomly from this list.
This is much more efficient than any other way.

So you would do something like this the first time the program runs:

Dim Permutation(24) As String
Permutation(0) = "1234"
Permutation(1) = "1243"
Permutation(2) = "1324"
Permutation(3) = "1342"
Permutation(4) = "1432"
Permutation(5) = "1423"
Permutation(6) = "2134"
Permutation(7) = "2143"
Permutation(8) = "2314"
Permutation(9) = "2341"
Permutation(10) = "2431"
Permutation(11) = "2413"
Permutation(12) = "3124"
Permutation(13) = "3142"
Permutation(14) = "3214"
Permutation(15) = "3241"
Permutation(16) = "3412"
Permutation(17) = "3421"
Permutation(18) = "4123"
Permutation(19) = "4132"
Permutation(20) = "4213"
Permutation(21) = "4231"
Permutation(22) = "4312"
Permutation(23) = "4321"

(I have used strings here for ease of demonstration only, and assume
an array base of zero [Option Base 0])

Then your subroutine would select one of these 24 elements at random,
and apply the order chosen.
I will leave it to you work out the mechanics...


or.. more simply and elegantly.. pick a random number from 1 to 24..

then u can sieve through the # to find out which permutation it is..

if it's in the first 6.. 1 is first
second 6 2 is first
etc.

within that group of six..
if it's in the first 2.. etc.

in the 2.. the order of the two will determine the order of the last two...
it's very simple..


I agree.

But for time critical routines, there's no substitute for a
pre-populated table.
I don't implement it as above, of course, but as an array of arrays.
One random look-up gives you the order without having to go through
seiving ranges.
They are "Pre-seived" as it were.

But you are right: your way is more elegant,
but I dispute that it is "simpler" to actually implement.

Nov 13 '05 #10

P: n/a
On 16 Sep 2004 11:47:59 -0700, gj******@hotmail.com (G.J. v.d. Kamp)
wrote:
Here's what i came up with:

I assume the table with the questions has the following format:

Id
Question
RightAnswer
WrongAnswer1
WrongAnswer2
WrongAnswer3

Now the following query will give you the answers to question 1 in
random order:

SELECT Answer, Correct
FROM
(SELECT RND() AS Position, RightAnswer AS Answer, True AS Correct FROM
Questions WHERE Id = 1
UNION ALL
SELECT RND() AS Position, WrongAnswer1, False AS Correct FROM
Questions WHERE Id = 1
UNION ALL
SELECT RND() AS Position, WrongAnswer2, False AS Correct FROM
Questions WHERE Id = 1
UNION ALL SELECT RND() AS Position, WrongAnswer3, False AS Correct
FROM Questions WHERE Id = 1)
ORDER BY Position;

I hardcoded question 1 in there to keep it simple, when you open this
in VBA you replace that with the Id of the question you want to ask.
It almost as lengthy as the other alternative, it just looks more
'pure' to me.

Regards,

GJ


Once again, an interesting approach.
But if it's elegance and generality that you are looking for, you
can't beat recursion!

Here's a recursive class (in VB6) that can permute an arbitrarily
sized collection of arbitrary objects.
You can't get more general than that!
It's not all that long, considering what it does.

(If you want to translate this into VB.net, be careful about the
different way collections are based)

'Class: Permutations
'(C) Copyright 2001, Michael Gray
' Generate permutations of the CollectionsToPermute in the
CollectionsToPermute collection.
' Return the result through a collection of collections that each hold
a unique permutation.
Property Get GeneratePermutations(ByVal CollectionsToPermute As
Collection) As Collection
Dim num_CollectionsToPermute As Integer
Dim I As Integer
Dim J As Integer
Dim K As Integer
Dim FirstValue As Variant
Dim NewPermutations As Collection
Dim ResultCollection As Collection
Dim NewResultCollection As Collection

' See if there is only one value.
If CollectionsToPermute.Count = 1 Then
' Return a collection containing one permutation equal to the
single value.
Set ResultCollection = New Collection
ResultCollection.Add New Collection
ResultCollection.Item(1).Add CollectionsToPermute.Item(1)
Set GeneratePermutations = ResultCollection
Exit Property
End If

' Build permutations starting with each possible first item.
Set ResultCollection = New Collection
num_CollectionsToPermute = CollectionsToPermute.Count
For I = 1 To num_CollectionsToPermute
' Save this value.
FirstValue = CollectionsToPermute.Item(I)
' Remove the item.
CollectionsToPermute.Remove I
' Generate the permutations of the remaining
CollectionsToPermute.
Set NewPermutations =
GeneratePermutations(CollectionsToPermute)
' Make permutations by adding FirstValue to the beginning of
each of the new permutations.
For J = 1 To NewPermutations.Count
' Add the first item.
Set NewResultCollection = New Collection
NewResultCollection.Add FirstValue
' Add the rest of the items in the jth new permutation.
For K = 1 To NewPermutations(J).Count
NewResultCollection.Add NewPermutations(J).Item(K)
Next K
' Add this new permutation to the ResultCollection.
ResultCollection.Add NewResultCollection
Next J
' Restore the removed value.
If I > CollectionsToPermute.Count Then
CollectionsToPermute.Add FirstValue
Else
CollectionsToPermute.Add FirstValue, , I
End If
Next I
' Return the ResultCollection.
Set GeneratePermutations = ResultCollection
End Property

Nov 13 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.