Random Positioning of Radio Form Items?

G

ga rg

I made a short multiple choice quiz in ruby using erb in an rhtml page.

I have an array of arrays:

@allanswers = [["thailand", "jamaica", "nairobi", "indonesia"],
["lincoln", "kennedy", "roosevelt", "reagan"],
["brazil", "canada", "columbia", "peru"],
["nelson", "bart", "kennedy", "jack"]]

and then I want to have the multiple choice options in a random order.
Meaning that every time a person refreshes the page, the order of the
choices changes.

<% 4.times do |num|%>
<INPUT type="radio" name="answer" value="<%=first =
@allanswers[@questionNum][rand(num)]%>"> <%=first%> <BR>
<%end%>

where @allanswers is the array of arrays.
@questionNum is an iterator in the code elsewhere

The obvious problem with this method is that the rand(num) ends up
giving duplicate options. What elegant solution can you think for this
problem? Basically, I just want to get random but unique numbers.

Thank you for reading.
 
A

ara.t.howard

I made a short multiple choice quiz in ruby using erb in an rhtml
page.

I have an array of arrays:

@allanswers = [["thailand", "jamaica", "nairobi", "indonesia"],
["lincoln", "kennedy", "roosevelt", "reagan"],
["brazil", "canada", "columbia", "peru"],
["nelson", "bart", "kennedy", "jack"]]

and then I want to have the multiple choice options in a random order.
Meaning that every time a person refreshes the page, the order of the
choices changes.

<% 4.times do |num|%>
<INPUT type="radio" name="answer" value="<%=first =
@allanswers[@questionNum][rand(num)]%>"> <%=first%> <BR>
<%end%>

where @allanswers is the array of arrays.
@questionNum is an iterator in the code elsewhere

The obvious problem with this method is that the rand(num) ends up
giving duplicate options. What elegant solution can you think for this
problem? Basically, I just want to get random but unique numbers.

Thank you for reading.

<% @allanswers[@questionNum].sort_by{ rand }.each do

...
...

a @ http://codeforpeople.com/
 
G

ga rg

Thank you :) I didn't know about .sort_by{ rand }



ara.t.howard said:
and then I want to have the multiple choice options in a random order.

The obvious problem with this method is that the rand(num) ends up
giving duplicate options. What elegant solution can you think for this
problem? Basically, I just want to get random but unique numbers.

Thank you for reading.

<% @allanswers[@questionNum].sort_by{ rand }.each do

...
...

a @ http://codeforpeople.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,055
Latest member
RacheleCar

Latest Threads

Top