quick opinion on math.random

classic Classic list List threaded Threaded
34 messages Options
12
Reply | Threaded
Open this post in threaded view
|

quick opinion on math.random

Nathan Rixham
general code related.

I just ran in to this line of code
x = Math.random() + Math.random() + Math.random() + Math.random();

and my immediate thought was why not just:
x = Math.random()*4;

can anybody give me any good reason why the 4 calls should be used -
logic dictates that its not going to be any "more random" than random
surely..

just seemed an odd thing to do imho

regards!

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

jlm@justinfront.net
Maybe addition is faster?
On 23 Jun 2009, at 22:56, Nathan Rixham wrote:

> general code related.
>
> I just ran in to this line of code
> x = Math.random() + Math.random() + Math.random() + Math.random();
>
> and my immediate thought was why not just:
> x = Math.random()*4;
>
> can anybody give me any good reason why the 4 calls should be used -  
> logic dictates that its not going to be any "more random" than  
> random surely..
>
> just seemed an odd thing to do imho
>
> regards!
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

jlm@justinfront.net
In reply to this post by Nathan Rixham
Actually maybe its more random... random is not so random, but 4 of  
them...
On 23 Jun 2009, at 22:56, Nathan Rixham wrote:

> general code related.
>
> I just ran in to this line of code
> x = Math.random() + Math.random() + Math.random() + Math.random();
>
> and my immediate thought was why not just:
> x = Math.random()*4;
>
> can anybody give me any good reason why the 4 calls should be used -  
> logic dictates that its not going to be any "more random" than  
> random surely..
>
> just seemed an odd thing to do imho
>
> regards!
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Jan_Flanders
In reply to this post by Nathan Rixham
For easier explaining, consider the old AS2 random function: random(max)

ten times:
var randomNr = random(10) + random(10) + random(10)...
randomNr can be 90 different numbers (0,1,2,3...90)

times ten:
var randomNr = random(10)  * 10
randomNr  can be only be 10 diferent numbers (0, 10, 20, 30...90)

Jan

On Tue, Jun 23, 2009 at 11:56 PM, Nathan Rixham <[hidden email]> wrote:
general code related.

I just ran in to this line of code
x = Math.random() + Math.random() + Math.random() + Math.random();

and my immediate thought was why not just:
x = Math.random()*4;

can anybody give me any good reason why the 4 calls should be used - logic dictates that its not going to be any "more random" than random surely..

just seemed an odd thing to do imho

regards!

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Nathan Rixham
but this is as3 and we're talking float 0 <= n < 1 - so the precision
loss is only 1 per 10* surely..

ad nez wrote:

> For easier explaining, consider the old AS2 random function: random(max)
>
> ten times:
> var randomNr = random(10) + random(10) + random(10)...
> randomNr can be 90 different numbers (0,1,2,3...90)
>
> times ten:
> var randomNr = random(10)  * 10
> randomNr  can be only be 10 diferent numbers (0, 10, 20, 30...90)
>
> Jan
>
> On Tue, Jun 23, 2009 at 11:56 PM, Nathan Rixham <[hidden email]> wrote:
>
>> general code related.
>>
>> I just ran in to this line of code
>> x = Math.random() + Math.random() + Math.random() + Math.random();
>>
>> and my immediate thought was why not just:
>> x = Math.random()*4;
>>
>> can anybody give me any good reason why the 4 calls should be used - logic
>> dictates that its not going to be any "more random" than random surely..
>>
>> just seemed an odd thing to do imho
>>
>> regards!
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>>
>


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

jlm@justinfront.net
But as I said the cumulative random will I suspect be more random  
irrespective of precision.. random is an algorithm that approximates  
random, not sure if it uses time as a seed... but it is not really  
random.

On 23 Jun 2009, at 23:29, Nathan Rixham wrote:

> but this is as3 and we're talking float 0 <= n < 1 - so the  
> precision loss is only 1 per 10* surely..
>
> ad nez wrote:
>> For easier explaining, consider the old AS2 random function:  
>> random(max)
>> ten times:
>> var randomNr = random(10) + random(10) + random(10)...
>> randomNr can be 90 different numbers (0,1,2,3...90)
>> times ten:
>> var randomNr = random(10)  * 10
>> randomNr  can be only be 10 diferent numbers (0, 10, 20, 30...90)
>> Jan
>> On Tue, Jun 23, 2009 at 11:56 PM, Nathan Rixham <[hidden email]>  
>> wrote:
>>> general code related.
>>>
>>> I just ran in to this line of code
>>> x = Math.random() + Math.random() + Math.random() + Math.random();
>>>
>>> and my immediate thought was why not just:
>>> x = Math.random()*4;
>>>
>>> can anybody give me any good reason why the 4 calls should be used  
>>> - logic
>>> dictates that its not going to be any "more random" than random  
>>> surely..
>>>
>>> just seemed an odd thing to do imho
>>>
>>> regards!
>>>
>>> --
>>> haXe - an open source web programming language
>>> http://haxe.org
>>>
>
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

James W. Hofmann
In reply to this post by jlm@justinfront.net
Adding four randoms is like summing four dice. You'll get a bell-curve  
distribution that tends to average 2, and is weighted against values  
near 0 or 4. Multiplying by 4 instead will give you a linear  
distribution.

(If you're having trouble imagining this, think of the sum of 10,000  
calls to Math.random(). It'll be about 5000, with a relatively small  
variation.)

Quoting "Justin Lawerance Mills" <[hidden email]>:

> Maybe addition is faster?
> On 23 Jun 2009, at 22:56, Nathan Rixham wrote:
>
>> general code related.
>>
>> I just ran in to this line of code
>> x = Math.random() + Math.random() + Math.random() + Math.random();
>>
>> and my immediate thought was why not just:
>> x = Math.random()*4;
>>
>> can anybody give me any good reason why the 4 calls should be used  
>> - logic dictates that its not going to be any "more random" than  
>> random surely..
>>
>> just seemed an odd thing to do imho
>>
>> regards!
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
> --
> haXe - an open source web programming language
> http://haxe.org
>



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Martijn Loots
And yes, the right answer came through... :)

On Tue, 23 Jun 2009, James Hofmann wrote:

> Adding four randoms is like summing four dice. You'll get a bell-curve
> distribution that tends to average 2, and is weighted against values near 0
> or 4. Multiplying by 4 instead will give you a linear distribution.
>
> (If you're having trouble imagining this, think of the sum of 10,000 calls to
> Math.random(). It'll be about 5000, with a relatively small variation.)
>
> Quoting "Justin Lawerance Mills" <[hidden email]>:
>
>> Maybe addition is faster?
>> On 23 Jun 2009, at 22:56, Nathan Rixham wrote:
>>
>>> general code related.
>>>
>>> I just ran in to this line of code
>>> x = Math.random() + Math.random() + Math.random() + Math.random();
>>>
>>> and my immediate thought was why not just:
>>> x = Math.random()*4;
>>>
>>> can anybody give me any good reason why the 4 calls should be used - logic
>>> dictates that its not going to be any "more random" than random surely..
>>>
>>> just seemed an odd thing to do imho
>>>
>>> regards!
>>>
>>> --
>>> haXe - an open source web programming language
>>> http://haxe.org
>>
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>>
>
>
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
-Martijn    @..@    ( Martijn Loots       -  Hengelo  [NL] )
-          (`--')   ( martijn<@>cosix.com -  www.cosix.com )
-         ( >__< )  ----------------------------------------
-         ^^^  ^^^  (   Netwerken, Security, Open Source   )

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Chris Hecker
In reply to this post by James W. Hofmann

It's probably a cheesy attempt at a gaussian.

Should use the Box-Muller transform instead.

Chris

James Hofmann wrote:

> Adding four randoms is like summing four dice. You'll get a bell-curve
> distribution that tends to average 2, and is weighted against values
> near 0 or 4. Multiplying by 4 instead will give you a linear distribution.
>
> (If you're having trouble imagining this, think of the sum of 10,000
> calls to Math.random(). It'll be about 5000, with a relatively small
> variation.)
>
> Quoting "Justin Lawerance Mills" <[hidden email]>:
>
>> Maybe addition is faster?
>> On 23 Jun 2009, at 22:56, Nathan Rixham wrote:
>>
>>> general code related.
>>>
>>> I just ran in to this line of code
>>> x = Math.random() + Math.random() + Math.random() + Math.random();
>>>
>>> and my immediate thought was why not just:
>>> x = Math.random()*4;
>>>
>>> can anybody give me any good reason why the 4 calls should be used -
>>> logic dictates that its not going to be any "more random" than random
>>> surely..
>>>
>>> just seemed an odd thing to do imho
>>>
>>> regards!
>>>
>>> --
>>> haXe - an open source web programming language
>>> http://haxe.org
>>
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>>
>
>
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Hugh Sanderson-2
In reply to this post by Nathan Rixham
Summing 4 uniform random numbers will produce something that is more  
gaussian distributed.

Consider 2 dice: random(6) + random(6) != random(12)

Or, to get something close to 0 with 4 numbers requires
EACH number must be close to 0.

Hugh

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Lee Sylvester
In reply to this post by jlm@justinfront.net
I believe multiplication is faster than addition. The fastest, of
course, being Math.random() << 2, which equates to Math.random() * 4
(but I believe still faster).

Lee


Justin Lawerance Mills wrote:

> Maybe addition is faster?
> On 23 Jun 2009, at 22:56, Nathan Rixham wrote:
>
>> general code related.
>>
>> I just ran in to this line of code
>> x = Math.random() + Math.random() + Math.random() + Math.random();
>>
>> and my immediate thought was why not just:
>> x = Math.random()*4;
>>
>> can anybody give me any good reason why the 4 calls should be used -
>> logic dictates that its not going to be any "more random" than random
>> surely..
>>
>> just seemed an odd thing to do imho
>>
>> regards!
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Ben Pedersen
I might be way off target, but most Math.random()  implementations I've seen uses current system time as a seed, which means you could risk getting the same result 4 times if you call Math.random() that many times within the same millisecond.

2009/6/24 Lee McColl Sylvester <[hidden email]>
I believe multiplication is faster than addition. The fastest, of course, being Math.random() << 2, which equates to Math.random() * 4 (but I believe still faster).

Lee



Justin Lawerance Mills wrote:
Maybe addition is faster?
On 23 Jun 2009, at 22:56, Nathan Rixham wrote:

general code related.

I just ran in to this line of code
x = Math.random() + Math.random() + Math.random() + Math.random();

and my immediate thought was why not just:
x = Math.random()*4;

can anybody give me any good reason why the 4 calls should be used - logic dictates that its not going to be any "more random" than random surely..

just seemed an odd thing to do imho

regards!

--
haXe - an open source web programming language
http://haxe.org




--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Ian Liu Rodrigues
Lee, I don't think that would happen, since random is a sequence of numbers which seems uncorrelated. The seed is a number which is used to determine the first element of the sequence, so random function don't produce the same sequence every time. I guess it is non-sense to seed the random sequence whenever random is called, although it might be good to seed it sometimes.

My half cent,
Ian L.

On Wed, Jun 24, 2009 at 7:53 AM, Ben Pedersen <[hidden email]> wrote:
I might be way off target, but most Math.random()  implementations I've seen uses current system time as a seed, which means you could risk getting the same result 4 times if you call Math.random() that many times within the same millisecond.

2009/6/24 Lee McColl Sylvester <[hidden email]>

I believe multiplication is faster than addition. The fastest, of course, being Math.random() << 2, which equates to Math.random() * 4 (but I believe still faster).

Lee



Justin Lawerance Mills wrote:
Maybe addition is faster?
On 23 Jun 2009, at 22:56, Nathan Rixham wrote:

general code related.

I just ran in to this line of code
x = Math.random() + Math.random() + Math.random() + Math.random();

and my immediate thought was why not just:
x = Math.random()*4;

can anybody give me any good reason why the 4 calls should be used - logic dictates that its not going to be any "more random" than random surely..

just seemed an odd thing to do imho

regards!

--
haXe - an open source web programming language
http://haxe.org




--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Lee Sylvester
I think you meant Ben, not Lee ;-)

Lee


Ian Liu wrote:

> Lee, I don't think that would happen, since random is a sequence of
> numbers which seems uncorrelated. The seed is a number which is used
> to determine the first element of the sequence, so random function
> don't produce the same sequence every time. I guess it is non-sense to
> seed the random sequence whenever random is called, although it might
> be good to seed it sometimes.
>
> My half cent,
> Ian L.
>
> On Wed, Jun 24, 2009 at 7:53 AM, Ben Pedersen <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     I might be way off target, but most Math.random()  implementations
>     I've seen uses current system time as a seed, which means you
>     could risk getting the same result 4 times if you call
>     Math.random() that many times within the same millisecond.
>
>     2009/6/24 Lee McColl Sylvester <[hidden email]
>     <mailto:[hidden email]>>
>
>         I believe multiplication is faster than addition. The fastest,
>         of course, being Math.random() << 2, which equates to
>         Math.random() * 4 (but I believe still faster).
>
>         Lee
>
>
>
>         Justin Lawerance Mills wrote:
>
>             Maybe addition is faster?
>             On 23 Jun 2009, at 22:56, Nathan Rixham wrote:
>
>                 general code related.
>
>                 I just ran in to this line of code
>                 x = Math.random() + Math.random() + Math.random() +
>                 Math.random();
>
>                 and my immediate thought was why not just:
>                 x = Math.random()*4;
>
>                 can anybody give me any good reason why the 4 calls
>                 should be used - logic dictates that its not going to
>                 be any "more random" than random surely..
>
>                 just seemed an odd thing to do imho
>
>                 regards!
>
>                 --
>                 haXe - an open source web programming language
>                 http://haxe.org
>
>
>
>
>
>         --
>         haXe - an open source web programming language
>         http://haxe.org
>
>
>
>     --
>     haXe - an open source web programming language
>     http://haxe.org
>
>


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Ben Pedersen
It would seem like it yes. :-)

And yes, I just tested it with the neko target, and it does produce different values. So you would do it for the bell-curve distribution, I'd say.

My confusion came from having worked with Math.random() and the Random class in java, and afaik the java implementation does produce the seed by looking at current system time.

Cheers.

2009/6/24 Lee McColl Sylvester <[hidden email]>
I think you meant Ben, not Lee ;-)

Lee


Ian Liu wrote:
Lee, I don't think that would happen, since random is a sequence of numbers which seems uncorrelated. The seed is a number which is used to determine the first element of the sequence, so random function don't produce the same sequence every time. I guess it is non-sense to seed the random sequence whenever random is called, although it might be good to seed it sometimes.

My half cent,
Ian L.

On Wed, Jun 24, 2009 at 7:53 AM, Ben Pedersen <[hidden email] <mailto:[hidden email]>> wrote:

   I might be way off target, but most Math.random()  implementations
   I've seen uses current system time as a seed, which means you
   could risk getting the same result 4 times if you call
   Math.random() that many times within the same millisecond.

   2009/6/24 Lee McColl Sylvester <[hidden email]
   <mailto:[hidden email]>>


       I believe multiplication is faster than addition. The fastest,
       of course, being Math.random() << 2, which equates to
       Math.random() * 4 (but I believe still faster).

       Lee



       Justin Lawerance Mills wrote:

           Maybe addition is faster?
           On 23 Jun 2009, at 22:56, Nathan Rixham wrote:

               general code related.

               I just ran in to this line of code
               x = Math.random() + Math.random() + Math.random() +
               Math.random();

               and my immediate thought was why not just:
               x = Math.random()*4;

               can anybody give me any good reason why the 4 calls
               should be used - logic dictates that its not going to
               be any "more random" than random surely..

               just seemed an odd thing to do imho

               regards!

               --                haXe - an open source web programming language
               http://haxe.org





       --        haXe - an open source web programming language
       http://haxe.org



   --
   haXe - an open source web programming language
   http://haxe.org




--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Memmie Lenglet
In reply to this post by Nathan Rixham
It's a non-unifmrom distribution of random value (probability).

See this demo for graphical representation.
http://memmie.lenglet.name/?p=24.

Based on 1M value with red line for average of all values and pink for average of probability
Click toi change with differents distributions.

Nathan Rixham a écrit :

> general code related.
>
> I just ran in to this line of code
> x = Math.random() + Math.random() + Math.random() + Math.random();
>
> and my immediate thought was why not just:
> x = Math.random()*4;
>
> can anybody give me any good reason why the 4 calls should be used -
> logic dictates that its not going to be any "more random" than random
> surely..
>
> just seemed an odd thing to do imho
>
> regards!
>

--
haXe - an open source web programming language
http://haxe.org

memmie.vcf (165 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Ian Liu Rodrigues
Hah, I meant Ben, sorry ;)

On Wed, Jun 24, 2009 at 8:41 AM, Memmie Lenglet <[hidden email]> wrote:
It's a non-unifmrom distribution of random value (probability).

See this demo for graphical representation.
http://memmie.lenglet.name/?p=24.

Based on 1M value with red line for average of all values and pink for average of probability
Click toi change with differents distributions.

Nathan Rixham a écrit :

general code related.

I just ran in to this line of code
x = Math.random() + Math.random() + Math.random() + Math.random();

and my immediate thought was why not just:
x = Math.random()*4;

can anybody give me any good reason why the 4 calls should be used - logic dictates that its not going to be any "more random" than random surely..

just seemed an odd thing to do imho

regards!



--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Nathan Rixham
In reply to this post by Ben Pedersen
Thanks for the replies everybody, and the explanations - hate to say I
was already aware but.. anyways it was a very specific question, and I'm
still convinced that Math.random()*4 is a better approach; there will be
a tiny loss of precision and you'll never get a 0.000000000001 - but
that's no major loss in the circumstances.

thanks again and great explanation mr hofmann!


Ben Pedersen wrote:

> It would seem like it yes. :-)
>
> And yes, I just tested it with the neko target, and it does produce
> different values. So you would do it for the bell-curve distribution, I'd
> say.
>
> My confusion came from having worked with Math.random() and the Random class
> in java, and afaik the java implementation does produce the seed by looking
> at current system time.
>
> Cheers.
>
> 2009/6/24 Lee McColl Sylvester <[hidden email]>
>
>> I think you meant Ben, not Lee ;-)
>>
>> Lee
>>
>>
>> Ian Liu wrote:
>>
>>> Lee, I don't think that would happen, since random is a sequence of
>>> numbers which seems uncorrelated. The seed is a number which is used to
>>> determine the first element of the sequence, so random function don't
>>> produce the same sequence every time. I guess it is non-sense to seed the
>>> random sequence whenever random is called, although it might be good to seed
>>> it sometimes.
>>>
>>> My half cent,
>>> Ian L.
>>>
>>> On Wed, Jun 24, 2009 at 7:53 AM, Ben Pedersen <[hidden email] <mailto:
>>> [hidden email]>> wrote:
>>>
>>>    I might be way off target, but most Math.random()  implementations
>>>    I've seen uses current system time as a seed, which means you
>>>    could risk getting the same result 4 times if you call
>>>    Math.random() that many times within the same millisecond.
>>>
>>>    2009/6/24 Lee McColl Sylvester <[hidden email]
>>>    <mailto:[hidden email]>>
>>>
>>>
>>>        I believe multiplication is faster than addition. The fastest,
>>>        of course, being Math.random() << 2, which equates to
>>>        Math.random() * 4 (but I believe still faster).
>>>
>>>        Lee
>>>
>>>
>>>
>>>        Justin Lawerance Mills wrote:
>>>
>>>            Maybe addition is faster?
>>>            On 23 Jun 2009, at 22:56, Nathan Rixham wrote:
>>>
>>>                general code related.
>>>
>>>                I just ran in to this line of code
>>>                x = Math.random() + Math.random() + Math.random() +
>>>                Math.random();
>>>
>>>                and my immediate thought was why not just:
>>>                x = Math.random()*4;
>>>
>>>                can anybody give me any good reason why the 4 calls
>>>                should be used - logic dictates that its not going to
>>>                be any "more random" than random surely..
>>>
>>>                just seemed an odd thing to do imho
>>>
>>>                regards!
>>>
>>>                --                haXe - an open source web programming
>>> language
>>>                http://haxe.org
>>>
>>>
>>>
>>>
>>>
>>>        --        haXe - an open source web programming language
>>>        http://haxe.org
>>>
>>>
>>>
>>>    --
>>>    haXe - an open source web programming language
>>>    http://haxe.org
>>>
>>>
>>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>>
>


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

edA-qa mort-ora-y
In reply to this post by Nathan Rixham
Nathan Rixham wrote:
> I just ran in to this line of code
> x = Math.random() + Math.random() + Math.random() + Math.random();
> and my immediate thought was why not just:
> x = Math.random()*4;

Well, * 4 limits the numbers which can be created.  Consider that if
random produced only integers * 4 would result in only multiples of
four.  So * 4 limits the resulting space.

Look at it another way.  Let's assume random is generated 31 bits of
random.  If you multiply by 4 your maximum value can not be made up of
33 bits.  But of those 33 bits only 31 are random, whereas if you add
four random numbers together you'll get all 33 bits of random.

Having said that, I can't think of many situations that would warrant
it.  Plus, if your bit spaces in total is only 31 bits it won't make any
difference how many you add together (The top bits are just stripped --
this applies even for float).


--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

BigTPoker uses haXe and DHLIB
        http://BigTPoker.com/?source=haxe-list

The dis-Emi-A haXe Library
        http://wiki.disemia.com/HaXe
       
A full set of tools, classes, and support facilities aimed at
simplifying and expediting game creation in Flash 9.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


--
haXe - an open source web programming language
http://haxe.org

signature.asc (260 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: quick opinion on math.random

Mark de Bruijn | Dykam
But, as it returns an floating point value, is Math.Random 4 times more random? Doesn't it use the whole precision range already, where the *4 just increases the factor.

On Thu, Jun 25, 2009 at 1:34 PM, edA-qa mort-ora-y <[hidden email]> wrote:
Nathan Rixham wrote:
> I just ran in to this line of code
> x = Math.random() + Math.random() + Math.random() + Math.random();
> and my immediate thought was why not just:
> x = Math.random()*4;

Well, * 4 limits the numbers which can be created.  Consider that if
random produced only integers * 4 would result in only multiples of
four.  So * 4 limits the resulting space.

Look at it another way.  Let's assume random is generated 31 bits of
random.  If you multiply by 4 your maximum value can not be made up of
33 bits.  But of those 33 bits only 31 are random, whereas if you add
four random numbers together you'll get all 33 bits of random.

Having said that, I can't think of many situations that would warrant
it.  Plus, if your bit spaces in total is only 31 bits it won't make any
difference how many you add together (The top bits are just stripped --
this applies even for float).


--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

BigTPoker uses haXe and DHLIB
       http://BigTPoker.com/?source=haxe-list

The dis-Emi-A haXe Library
       http://wiki.disemia.com/HaXe

A full set of tools, classes, and support facilities aimed at
simplifying and expediting game creation in Flash 9.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


--
haXe - an open source web programming language
http://haxe.org



--
Mark

--
haXe - an open source web programming language
http://haxe.org
12