[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Eric Hodel

11/28/2006 7:14:00 PM

On Nov 28, 2006, at 1046 , 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?

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

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...