Array#choice on 1.8.7

  • Thread starter Eustaquio 'TaQ' Rangel
  • Start date
E

Eustaquio 'TaQ' Rangel

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.
 
P

Peña, Botp

From: Eustaquio 'TaQ' Rangel [mailto:[email protected]]=20
# I just tested on one machine, but seems that if we make some call to=20
# rand before using Array#choice we can have better random=20
# values, check it out:
# [taq@~/code/ruby]ruby choice.rb
# 5
# 5
# 1

what do you want, no duplicates?

kind regards -botp
 
T

Timm Mason (c)

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 =3D {}
each_pair {|k,v| attrs[k] =3D builder.trunc!(v.to_s)
unless v.nil? || v.to_s.empty?}
builder.testcase(attrs) do
failures.each do |failure|
builder.failure:)type =3D>
builder.trunc!(failure.name), :message =3D>
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 =3D> [ :all_tests ]

task :all_tests =3D> ['ci:setup:testunit']

Rake::TestTask.new:)all_tests) do |t|
t.test_files =3D FileList['test/*test.rb']
t.verbose =3D true
t.warning =3D false
end
 
E

Eustaquio 'TaQ' Rangel

Peña said:
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.
 
M

Michael Fellinger

T24gV2VkLCBKdW4gNCwgMjAwOCBhdCAxMTowMSBBTSwgRXVzdGFxdWlvICdUYVEnIFJhbmdlbAo8
ZXVzdGFxdWlvcmFuZ2VsQGdtYWlsLmNvbT4gd3JvdGU6Cj4gUGXDsWEgd3JvdGU6Cj4+Cj4+IHdo
YXQgZG8geW91IHdhbnQsIG5vIGR1cGxpY2F0ZXM/Cj4KPiBObywgcGxlYXNlIGNoZWNrIHRoYXQg
dGhlIHRocmVlIHRpbWVzIEkgcnVuIHRoZSBwcm9ncmFtIHdpdGhvdXQgcmFuZCwgdGhlCj4gcmVz
dWx0cyBhcmUgdGhlIHNhbWUgYWxsIHRoZSB0aW1lcy4gV2l0aCByYW5kIGJlZm9yZSBjaG9pY2Us
IG9uIHRoZSB0aHJlZQo+IHRpbWVzIHRoZSByZXN1bHRzIHdlcmUgZGlmZmVyZW50LCBvbiBhIG1v
cmUgcmFuZG9tIHdheS4KCkkgZ3Vlc3MgeW91IHNob3VsZCBjYWxsIHNyYW5kIGJlZm9yZSB1c2lu
ZyB0aGlzICh3aGljaCBjYWxsaW5nIHJhbmQKd2lsbCBkbyBhcyB3ZWxsKS4KRm9yIHNvbWUgcmVh
c29uIG5laXRoZXIgdGhlIG5ldyBBcnJheSNzaHVmZmxlIG5vciBBcnJheSNjaG9pY2Ugc2VlbXMK
dG8gZG8gdGhpcyByaWdodCBub3cuIE1pZ2h0IGp1c3QgYXMgd2VsbCBiZSBhIGJ1Zy4KCl4gbWFu
dmVydQo=
 

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
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top