# Random Positioning of Radio Form Items?

**URL:** <https://www.ruby-forum.com/t/random-positioning-of-radio-form-items/123835>\
**Category:** Ruby\
**Created:** [December 21, 2007, 4:03am UTC](https://www.ruby-forum.com/t/random-positioning-of-radio-form-items/123835 "2007-12-21T04:03:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [December 21, 2007, 4:03am UTC](https://www.ruby-forum.com/t/random-positioning-of-radio-form-items/123835/1 "2007-12-21T04:03:27Z")

</div>

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|%\>  
 \<%=first%\>   
  
\<%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.

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [December 21, 2007, 5:43am UTC](https://www.ruby-forum.com/t/random-positioning-of-radio-form-items/123835/2 "2007-12-21T05:43:37Z")

</div>

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://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [December 21, 2007, 6:16am UTC](https://www.ruby-forum.com/t/random-positioning-of-radio-form-items/123835/3 "2007-12-21T06:16:23Z")

</div>

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://codeforpeople.com/](http://codeforpeople.com/)
