[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Random Positioning of Radio Form Items?

ga rg

12/21/2007 3:03:00 AM

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.
--
Posted via http://www.ruby-....

2 Answers

ara.t.howard

12/21/2007 4:43:00 AM

0


On Dec 20, 2007, at 8:03 PM, ga rg wrote:

> 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://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




ga rg

12/21/2007 5:16:00 AM

0

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



ara.t.howard wrote:
> On Dec 20, 2007, at 8:03 PM, ga rg wrote:
>
>> 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://codeforp...

--
Posted via http://www.ruby-....