# Re: rand random. You've got to be kidding

**URL:** <https://www.ruby-forum.com/t/re-rand-random-youve-got-to-be-kidding/77552>\
**Category:** Ruby\
**Created:** [November 28, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/re-rand-random-youve-got-to-be-kidding/77552 "2006-11-28T19:48:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 28, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/re-rand-random-youve-got-to-be-kidding/77552/1 "2006-11-28T19:48:08Z")

</div>

In a message dated 11/28/2006 11:08:19 A.M. US Mountain Standard Tim,  
removed\_email\_address@domain.invalid 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.

Charlie

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 28, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/re-rand-random-youve-got-to-be-kidding/77552/2 "2006-11-28T20:14:16Z")

</div>

On Nov 28, 2006, at 1046 , removed\_email\_address@domain.invalid wrote:

> > > 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 }

> [[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

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 28, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/re-rand-random-youve-got-to-be-kidding/77552/3 "2006-11-28T20:18:16Z")

</div>

Your problem is with  
@deck.sort{rand}

## ------------------------------------------------------------- Array#sort array.sort -\> an\_array array.sort {| a,b | block } -\> an\_array

```
 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
