# Array#choice on 1.8.7

**URL:** <https://www.ruby-forum.com/t/array-choice-on-1-8-7/139462>\
**Category:** Ruby\
**Created:** [June 4, 2008, 3:15am UTC](https://www.ruby-forum.com/t/array-choice-on-1-8-7/139462 "2008-06-04T03:15:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eustaquio\_STaQS\_Ran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@Eustaquio\_STaQS\_Ran](https://www.ruby-forum.com/u/Eustaquio_STaQS_Ran)\
**Post date:** [June 4, 2008, 3:15am UTC](https://www.ruby-forum.com/t/array-choice-on-1-8-7/139462/1 "2008-06-04T03:15:58Z")

</div>

Hi.

I just tested on one machine, but seems that if we make some call to  
rand before using Array#choice we can have better random values, check  
it out:

# choice.rb

a = (1…10).to\_a  
3.times { puts a.choice }

[taq@~/code/ruby]ruby choice.rb  
5  
5  
1  
[taq@~/code/ruby]ruby choice.rb  
9  
10  
2  
[taq@~/code/ruby]ruby choice.rb  
9  
10  
2

Inserting rand:

# choice.rb

a = (1…10).to\_a  
rand  
3.times { puts a.choice }

[taq@~/code/ruby]ruby choice.rb  
6  
8  
6  
[taq@~/code/ruby]ruby choice.rb  
3  
8  
4  
[taq@~/code/ruby]ruby choice.rb  
6  
2  
10

Is there any recommendation to call rand before using choice?

Thanks.

---

<div class="post-metadata">

**Author:** ![Eustaquio\_STaQS\_Ran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@Eustaquio\_STaQS\_Ran](https://www.ruby-forum.com/u/Eustaquio_STaQS_Ran)\
**Post date:** [June 4, 2008, 3:50am UTC](https://www.ruby-forum.com/t/array-choice-on-1-8-7/139462/2 "2008-06-04T03:50:30Z")

</div>

From: Eustaquio ‘TaQ’ Rangel [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# I just tested on one machine, but seems that if we make some call to

# rand before using Array#choice we can have better random

# values, check it out:

# [taq@~/code/ruby]ruby choice.rb

# 5

# 5

# 1

what do you want, no duplicates?

kind regards -botp

---

<div class="post-metadata">

**Author:** ![Eustaquio\_STaQS\_Ran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@Eustaquio\_STaQS\_Ran](https://www.ruby-forum.com/u/Eustaquio_STaQS_Ran)\
**Post date:** [June 4, 2008, 3:56am UTC](https://www.ruby-forum.com/t/array-choice-on-1-8-7/139462/3 "2008-06-04T03:56:57Z")

</div>

Hi, I was wondering if anyone has had any luck trying to extend  
CI::Reporter

Specifically, I wanted to change some formatting, so that failure  
messages are enclosed in CDATA blocks. I tried extending the to\_xml  
method in Rakefile.rb but it seems to have no effect, so clearly I am  
missing something…

My Rakefile is below. When I run it, everything happens as if I haven’t  
actually extended the method at all. Ie “YO!!!” does not appear in the  
XML output, and “this is broken!” never prints out. What am I missing  
here?

require ‘rake’  
require ‘rake/testtask’  
require ‘rake/clean’  
require ‘rubygems’  
gem ‘ci\_reporter’  
require ‘ci/reporter/rake/test\_unit’  
require ‘ci/reporter/core’

# modify ci\_reporter to put failure message in CDATA element # note this

is not working class CI::Reporter::TestUnit  
def to\_xml(builder)  
attrs = {}  
each\_pair {|k,v| attrs[k] = builder.trunc!(v.to\_s)  
unless v.nil? || v.to\_s.empty?}  
builder.testcase(attrs) do  
failures.each do |failure|  
builder.failure(:type =\>  
builder.trunc!(failure.name), :message =\>  
builder.trunc!(failure.message)) do  
builder.text!(“YO!!!” +  
failure.message + " (#{failure.name})\n")  
builder.text!(failure.location)  
end  
end  
end  
puts “this is broken!”  
end  
end

task :default =\> [:all\_tests]

task :all\_tests =\> [‘ci:setup:testunit’]

Rake::TestTask.new(:all\_tests) do |t|  
t.test\_files = FileList[‘test/\*test.rb’]  
t.verbose = true  
t.warning = false  
end

---

<div class="post-metadata">

**Author:** ![Eustaquio\_STaQS\_Ran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@Eustaquio\_STaQS\_Ran](https://www.ruby-forum.com/u/Eustaquio_STaQS_Ran)\
**Post date:** [June 4, 2008, 6:21am UTC](https://www.ruby-forum.com/t/array-choice-on-1-8-7/139462/4 "2008-06-04T06:21:26Z")

</div>

On Wed, Jun 4, 2008 at 11:01 AM, Eustaquio ‘TaQ’ Rangel  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> PeÃ±a wrote:
> 
> > what do you want, no duplicates?
> 
> No, please check that the three times I run the program without rand, the  
> results are the same all the times. With rand before choice, on the three  
> times the results were different, on a more random way.

I guess you should call srand before using this (which calling rand  
will do as well).  
For some reason neither the new Array#shuffle nor Array#choice seems  
to do this right now. Might just as well be a bug.

^ manveru

---

<div class="post-metadata">

**Author:** ![Eustaquio\_STaQS\_Ran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@Eustaquio\_STaQS\_Ran](https://www.ruby-forum.com/u/Eustaquio_STaQS_Ran)\
**Post date:** [June 4, 2008, 4:02am UTC](https://www.ruby-forum.com/t/array-choice-on-1-8-7/139462/5 "2008-06-04T04:02:48Z")

</div>

Peña wrote:

> what do you want, no duplicates?

No, please check that the three times I run the program without rand,  
the results are the same all the times. With rand before choice, on the  
three times the results were different, on a more random way.
