PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > rand() v. rand(0.1) ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
rand() v. rand(0.1) ?

Réponse
 
LinkBack Outils de la discussion
Vieux 15/09/2007, 11h37   #1 (permalink)
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut rand() v. rand(0.1) ?

Is there any difference between calling rand() and rand(0.1)?
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 15/09/2007, 12h11   #2 (permalink)
Florian Frank
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rand() v. rand(0.1) ?

7stud -- wrote:
> Is there any difference between calling rand() and rand(0.1)?
>

------------------------------------------------------------ Kernel#rand
rand(max=0) => number
------------------------------------------------------------------------
Converts _max_ to an integer using max1 = max+.to_i.abs+. [...]

No.

--
Florian Frank


  Réponse avec citation
Vieux 15/09/2007, 12h35   #3 (permalink)
Chad Perrin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rand() v. rand(0.1) ?

On Sat, Sep 15, 2007 at 08:11:25PM +0900, Florian Frank wrote:
> 7stud -- wrote:
> >Is there any difference between calling rand() and rand(0.1)?
> >

> ------------------------------------------------------------ Kernel#rand
> rand(max=0) => number
> ------------------------------------------------------------------------
> Converts _max_ to an integer using max1 = max+.to_i.abs+. [...]
>
> No.


Speaking of which . . . obviously rand() doesn't produce a truly random
number, but it's reasonably close for some purposes. I'm curious about
just how far off it is, though -- because I'm curious about how
appropriate it is to use to simulate dice-rolling for gaming software (in
the "roleplaying game" sense of the term "gaming") in Ruby's
implementation. I'd prefer some kind of a general feel for it before I
write software to statistically analyze the output of millions of
iterations of rand() evaluations.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Dr. Ron Paul: "Liberty has meaning only if we still believe in it when
terrible things happen and a false government security blanket beckons."

  Réponse avec citation
Vieux 15/09/2007, 13h59   #4 (permalink)
Matthew Rudy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rand() v. rand(0.1) ?

7stud -- wrote:
> Is there any difference between calling rand() and rand(0.1)?


takes a look at the rdoc -
http://www.ruby-doc.org/core/classes...l.html#M005955

its defined as "rand(max=0)"
ie. calling rand() is exactly the same as rand(0)

also it "Converts max to an integer using max1 = max.to_i.abs"
ie. max = 0.1 -> max1 = 0.1.to_i.abs = 0
ie. rand() results in the same behaviour as rand(0.1)
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 15/09/2007, 14h43   #5 (permalink)
Chad Perrin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rand() v. rand(0.1) ?

On Sat, Sep 15, 2007 at 09:09:52PM +0900, Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: rand() v. rand(0.1) ?"
> on Sat, 15 Sep 2007 20:35:37 +0900, Chad Perrin <perrin@apotheon.com> writes:
>
> |Speaking of which . . . obviously rand() doesn't produce a truly random
> |number, but it's reasonably close for some purposes. I'm curious about
> |just how far off it is, though -- because I'm curious about how
> |appropriate it is to use to simulate dice-rolling for gaming software (in
> |the "roleplaying game" sense of the term "gaming") in Ruby's
> |implementation.
>
> It uses Mersenne Twister algorithm which has a period of 2**19937-1.


Excellent! Thank you for the quick and informative response.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Amazon.com interview candidate: "When C++ is your hammer, everything starts
to look like your thumb."

  Réponse avec citation
Vieux 15/09/2007, 20h54   #6 (permalink)
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rand() v. rand(0.1) ?

Matthew Rudy wrote:
> 7stud -- wrote:
>> Is there any difference between calling rand() and rand(0.1)?

>
> takes a look at the rdoc -
> http://www.ruby-doc.org/core/classes...l.html#M005955
>
> its defined as "rand(max=0)"
> ie. calling rand() is exactly the same as rand(0)
>
> also it "Converts max to an integer using max1 = max.to_i.abs"
> ie. max = 0.1 -> max1 = 0.1.to_i.abs = 0
> ie. rand() results in the same behaviour as rand(0.1)


That was my analysis too, however pickaxe2 has some code on p. 138 that
calls rand(0.1).
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 16/09/2007, 16h18   #7 (permalink)
Morton Goldberg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rand() v. rand(0.1) ?

On Sep 15, 2007, at 3:54 PM, 7stud -- wrote:

> That was my analysis too, however pickaxe2 has some code on p. 138
> that
> calls rand(0.1).


You've found a bug in the Pickaxe book. I'm guessing but I suspect that

sleep(rand(0.1))

should be

sleep(0.1 * rand)

That is, I think the idea was to sleep in the range 0.0 to 0.1
seconds, not 0.0 to 1.0 seconds.

Regards, Morton

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h41.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14357 seconds with 15 queries