In a message dated 11/28/2006 11:08:19 A.M. US Mountain Standard Tim,
brabuhr@gmail.com writes:
Surely it must be possible to initialize rand with current date and time.
Documents seem to indicate this is done with rand(0) or srand with no
value. I
have tried both methods and I get the same tired old sequence of numbers. I
would write my own random number generator except I don't know how to get a
numeric value out of Time.
Can you post the code that isn't working for you?
I was running the following in SciTE
class Dominoes
attr_writer :deck
def initialize(number)
@deck= Array.new
0.upto(number) {|i| 0.upto(i) {|j| @deck.push([i, j])}}
end
def shuffle! @deck = @deck.sort{rand}
p @deck
end
end
a=Dominoes.new(6)
a.shuffle!
The p @deck was added so I could see what was being done.
Every time I press F5 I get exactly the same result:
[[6, 6], [4, 4], [0, 0], [5, 0], [1, 1], ...
I get the same results if I wait a few minutes or come back the next day. I
get exactly the same results when I use fxri and freeRIDE. I am running Ruby
version 1.18.4.
In a message dated 11/28/2006 11:08:19 A.M. US Mountain Standard Tim,
brabuhr@gmail.com writes:
Surely it must be possible to initialize rand with current date and time.
Documents seem to indicate this is done with rand(0) or srand with no value. I
have tried both methods and I get the same tired old sequence of numbers. I
would write my own random number generator except I don't know how to get a
numeric value out of Time.
Can you post the code that isn't working for you?
Please quote your replies correctly.
I was running the following in SciTE
class Dominoes
attr_writer :deck
def initialize(number)
@deck= Array.new
0.upto(number) {|i| 0.upto(i) {|j| @deck.push([i, j])}}
end
def shuffle! @deck = @deck.sort{rand}
You want @deck = @deck.sort_by { rand }
p @deck
end
a=Dominoes.new(6)
a.shuffle!
The p @deck was added so I could see what was being done.
Every time I press F5 I get exactly the same result:
[[6, 6], [4, 4], [0, 0], [5, 0], [1, 1], ...
I get the same results if I wait a few minutes or come back the next day. I
get exactly the same results when I use fxri and freeRIDE. I am running Ruby
version 1.18.4.
Try:
10.times do puts rand end
then:
srand Time.now.to_i
10.times do puts rand end
···
On Nov 28, 2006, at 1046 , Smgspices@aol.com wrote:
--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant
------------------------------------------------------------------------
Returns a new array created by sorting _self_. Comparisons for the sort will be done using the +<=>+ operator or using an optional code block. The block implements a comparison between _a_ and _b_, returning -1, 0, or +1. See also +Enumerable#sort_by+.
a = [ "d", "a", "e", "c", "b" ] a.sort #=> ["a", "b", "c", "d", "e"] a.sort {|x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]
rand produces always produces values between 0 and 1 so it will always sort the same
so you need something like this
@deck.sort { if rand < 0.5 then -1; else 1; end }
Ken
Smgspices@aol.com wrote:
In a message dated 11/28/2006 11:08:19 A.M. US Mountain Standard Tim, brabuhr@gmail.com writes:
Surely it must be possible to initialize rand with current date and time.
Documents seem to indicate this is done with rand(0) or srand with no
value. I
have tried both methods and I get the same tired old sequence of numbers. I
would write my own random number generator except I don't know how to get a
numeric value out of Time.
Can you post the code that isn't working for you?
I was running the following in SciTE
class Dominoes
attr_writer :deck
def initialize(number)
@deck= Array.new
0.upto(number) {|i| 0.upto(i) {|j| @deck.push([i, j])}}
end
def shuffle! @deck = @deck.sort{rand}
p @deck
end
a=Dominoes.new(6)
a.shuffle!
The p @deck was added so I could see what was being done.
Every time I press F5 I get exactly the same result:
[[6, 6], [4, 4], [0, 0], [5, 0], [1, 1], ...
I get the same results if I wait a few minutes or come back the next day. I get exactly the same results when I use fxri and freeRIDE. I am running Ruby version 1.18.4.
Charlie