Re: [Flashcoders] Whats the deal with AS3 Math.random?

2006-11-27 Thread Ron Wheeler
Math.random generates psuedo-random numbers. If you want to have 
something that looks more random, try calling Math.random a random 
number of times before using the numbers.
Use a true random number to determine the number of times to call it in 
the initial run.
Suggestion: the modulus 100 of the number of milli-seconds since 
midnight (or some arbitrary date/time).
This should give you at least 100 different pseudo-ransom strings. 
increase the modulus to get more.


I believe that you can get hardware devices that will give you truly 
random numbers, if that is really required.


As John von Neumann said in 1961 "Anyone who considers arithmetical 
methods of producing random digits is of course, in a state of sin."


We are all forced to chose between lesser and greater sins by our need 
for economical solutions.


Ron

Sascha wrote:

Hi,

Why is it when I use Math.random to generate 10 random integers I get the
same pattern of numbers almost every time. Starting the SWF movie ten times
and I have 6-8 times the same pattern of numbers. What's going on there?
Almost looks as if the compiler sets pre-compiled numbers.

Using this ...

var str:String = "";

for (var i:int = 0; i < 10; i++)
{
str += Math.floor(Math.random() * 100) + ",
";
}
trace(str);

... and running the SWF movie ten times, I get this ...

88, 87, 35, 69, 73, 11, 93, 40, 39, 19, 
43, 69, 70, 96, 39, 97, 8, 74, 29, 65, 
50, 32, 82, 45, 39, 74, 96, 5, 7, 9, 
50, 32, 82, 45, 39, 74, 96, 5, 7, 9, 
50, 32, 82, 45, 39, 74, 96, 5, 7, 9, 
50, 32, 82, 45, 39, 74, 96, 5, 7, 9, 
50, 32, 82, 45, 39, 74, 96, 5, 7, 9, 
88, 87, 35, 69, 73, 11, 93, 40, 39, 19, 
50, 32, 82, 45, 39, 74, 96, 5, 7, 9, 
88, 87, 35, 69, 73, 11, 93, 40, 39, 19, 
  
This is the first time I experience this.  Is there any solution to fix

this?

Thanks,
Sascha


___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


  

___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


RE: [Flashcoders] Whats the deal with AS3 Math.random?

2006-11-27 Thread Danny Kodicek
 > Hi,
> 
> Why is it when I use Math.random to generate 10 random 
> integers I get the same pattern of numbers almost every time. 
> Starting the SWF movie ten times and I have 6-8 times the 
> same pattern of numbers. What's going on there?

Most randomisation algorithms are based on a 'seed' value which is passed
through a complicated function to produce a new result. This allows
programmes containing random numbers to be tested. I don't know about AS3,
but certainly up to now there's been no way to specify the random seed, and
by the look of your test, it's reset when Flash restarts (you might want to
compare the browser with the standalone player, though). 

You can specify the randomseed for the BitmapData.noise and perlinNoise
functions, so you could use these to create more variable data. 

Danny

___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


RE: [Flashcoders] Whats the deal with AS3 Math.random?

2006-11-27 Thread Merrill, Jason
I've noticed that my random functions aren't nearly as random as I would
like (I see annoying patterns all the time) - it seems the same set of
numbers pop up frequently for each session.  Good idea Ron.  

Jason Merrill
Bank of America 
Learning & Organizational Effectiveness
 
 
 
 
 

>>-Original Message-
>>From: [EMAIL PROTECTED] [mailto:flashcoders-
>>[EMAIL PROTECTED] On Behalf Of Ron Wheeler
>>Sent: Monday, November 27, 2006 9:45 AM
>>To: Flashcoders mailing list
>>Subject: Re: [Flashcoders] Whats the deal with AS3 Math.random?
>>
>>Math.random generates psuedo-random numbers. If you want to have
>>something that looks more random, try calling Math.random a random
>>number of times before using the numbers.
>>Use a true random number to determine the number of times to call it
in
>>the initial run.
>>Suggestion: the modulus 100 of the number of milli-seconds since
>>midnight (or some arbitrary date/time).
>>This should give you at least 100 different pseudo-ransom strings.
>>increase the modulus to get more.
>>
>>I believe that you can get hardware devices that will give you truly
>>random numbers, if that is really required.
>>
>>As John von Neumann said in 1961 "Anyone who considers arithmetical
>>methods of producing random digits is of course, in a state of sin."
>>
>>We are all forced to chose between lesser and greater sins by our need
>>for economical solutions.
>>
>>Ron
>>
>>Sascha wrote:
>>> Hi,
>>>
>>> Why is it when I use Math.random to generate 10 random integers I
get the
>>> same pattern of numbers almost every time. Starting the SWF movie
ten times
>>> and I have 6-8 times the same pattern of numbers. What's going on
there?
>>> Almost looks as if the compiler sets pre-compiled numbers.
>>>
>>> Using this ...
>>>
>>> var str:String = "";
>>>
>>> for (var i:int = 0; i < 10; i++)
>>> {
>>> str += Math.floor(Math.random() * 100) +
",
>>> ";
>>> }
>>> trace(str);
>>>
>>> ... and running the SWF movie ten times, I get this ...
>>>
>>> 88, 87, 35, 69, 73, 11, 93, 40, 39, 19,
>>> 43, 69, 70, 96, 39, 97, 8, 74, 29, 65,
>>> 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
>>> 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
>>> 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
>>> 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
>>> 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
>>> 88, 87, 35, 69, 73, 11, 93, 40, 39, 19,
>>> 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
>>> 88, 87, 35, 69, 73, 11, 93, 40, 39, 19,
>>>
>>> This is the first time I experience this.  Is there any solution to
fix
>>> this?
>>>
>>> Thanks,
>>> Sascha
>>>
>>>
>>> ___
>>> Flashcoders@chattyfig.figleaf.com
>>> To change your subscription options or search the archive:
>>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>>
>>> Brought to you by Fig Leaf Software
>>> Premier Authorized Adobe Consulting and Training
>>> http://www.figleaf.com
>>> http://training.figleaf.com
>>>
>>>
>>>
>>___
>>Flashcoders@chattyfig.figleaf.com
>>To change your subscription options or search the archive:
>>http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>>Brought to you by Fig Leaf Software
>>Premier Authorized Adobe Consulting and Training
>>http://www.figleaf.com
>>http://training.figleaf.com
___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


RE: [Flashcoders] Whats the deal with AS3 Math.random?

2006-11-27 Thread Sascha
Thanks Ron and Danny!
I knew that the random numbers are not truly random and that the seed cannot
be changed but the random number patterns I get now are pretty hefty, I
think this wasn't like that in AS2 times.
I will try to fumble with the date object to get a better seed.

Sascha



> -Original Message-
> From: [EMAIL PROTECTED] [mailto:flashcoders-
> [EMAIL PROTECTED] On Behalf Of Ron Wheeler
> Sent: Monday, 27 November, 2006 23:45
> To: Flashcoders mailing list
> Subject: Re: [Flashcoders] Whats the deal with AS3 Math.random?
> 
> Math.random generates psuedo-random numbers. If you want to have
> something that looks more random, try calling Math.random a random
> number of times before using the numbers.
> Use a true random number to determine the number of times to call it in
> the initial run.
> Suggestion: the modulus 100 of the number of milli-seconds since
> midnight (or some arbitrary date/time).
> This should give you at least 100 different pseudo-ransom strings.
> increase the modulus to get more.
> 
> I believe that you can get hardware devices that will give you truly
> random numbers, if that is really required.
> 
> As John von Neumann said in 1961 "Anyone who considers arithmetical
> methods of producing random digits is of course, in a state of sin."
> 
> We are all forced to chose between lesser and greater sins by our need
> for economical solutions.
> 
> Ron
> 
> Sascha wrote:
> > Hi,
> >
> > Why is it when I use Math.random to generate 10 random integers I get
the
> > same pattern of numbers almost every time. Starting the SWF movie ten
times
> > and I have 6-8 times the same pattern of numbers. What's going on there?
> > Almost looks as if the compiler sets pre-compiled numbers.
> >
> > Using this ...
> >
> > var str:String = "";
> >
> > for (var i:int = 0; i < 10; i++)
> > {
> > str += Math.floor(Math.random() * 100) + ",
> > ";
> > }
> > trace(str);
> >
> > ... and running the SWF movie ten times, I get this ...
> >
> > 88, 87, 35, 69, 73, 11, 93, 40, 39, 19,
> > 43, 69, 70, 96, 39, 97, 8, 74, 29, 65,
> > 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
> > 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
> > 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
> > 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
> > 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
> > 88, 87, 35, 69, 73, 11, 93, 40, 39, 19,
> > 50, 32, 82, 45, 39, 74, 96, 5, 7, 9,
> > 88, 87, 35, 69, 73, 11, 93, 40, 39, 19,
> >
> > This is the first time I experience this.  Is there any solution to fix
> > this?
> >
> > Thanks,
> > Sascha
> >
> >
> > ___
> > Flashcoders@chattyfig.figleaf.com
> > To change your subscription options or search the archive:
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
> > Brought to you by Fig Leaf Software
> > Premier Authorized Adobe Consulting and Training
> > http://www.figleaf.com
> > http://training.figleaf.com
> >
> >
> >
> ___
> Flashcoders@chattyfig.figleaf.com
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> 
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com

___
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


Re: [Flashcoders] Whats the deal with AS3 Math.random?

2006-11-27 Thread Jon Bradley
And yet another random number generator library (was posted at were- 
here.com a long long time ago). All open source, credit for use is  
nice though. :)


This one is a linear congruential generator. I've never tested the  
output against anything, so at best, this may or may not work  
properly for your needs.


cheers,

jon

Jon Bradley
VFX Artist / Animator
Post Central, Inc.
[EMAIL PROTECTED]
www.cherrycrushthemovie.com



/** BEGIN LCG.as */

/**
* Class com.jbradley.math.LCG
* AuthorJon Bradley
* Version   1.0
* Description 	Implementation of 48-bit Linear Congruential Pseudo- 
Random Number Generator
*  
 
---

* Last update: November 3, 2005 - Converted to AS 2.0
*  
 
---

* 48-bit Linear Congruential Algorithm
* Produces pseudo-random number which can be seeded manually or by a  
default time variable.

*
* Source: Computational Nuclear Physics Group, United States Government
* RNG - Random Number Generator algorithms
*
* This implementation is limited to the 48-bit version, which is  
still computationally
* inaccurate in Flash due to a 32-bit limitation. It is also not  
statistically a sound
* random number generator due to patterns in the low order sequences  
and is not a
* cryptographically secure algorithm. It can be made secure by using  
a cryptographic hash
* function to generate an initial random number state from a pair of  
integers (keys).

*
* The algorithm implementation is as described in the RNG library  
published by the
* Lawrence Livermore National Laboratory - http://nuclear.llnl.gov/ 
CNP/rng/rngman/

*
* Converted to Actionscript by Jon Bradley, 2003
*  
 
---

* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions  
are met:

*
* - Redistributions of source code must retain the above  
copyright notice,

*   this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above  
copyright
*   notice, this list of conditions and the following disclaimer  
in the
*   documentation and/or other materials provided with the  
distribution.

*
* - Neither the name of this software nor the names of its  
contributors
*   may be used to endorse or promote products derived from this  
software

*   without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  
"AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  
THE IMPLIED

* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS  
BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR  
CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR  
SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER  
CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR  
TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  
USE OF THIS

* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*  
 
---

*
* Usage:
*
*Default:
*rGen = new LCG();
*
*Specific seed - may use new Date.getTime() ):
*rGen = new LCG( 1 );
*
* Methods:
*
*init(t:Number)
*Initializes LCG with a time seed. If t is not supplied or
*is NaN, default is used.
*
*getSeed():Number
*Returns initial time seed used to start the system. Recall  
that the actual seed
*used in the algorithm is time&M, where M is 2.8^14. The  
returned number maybe

*be passed back in to init to restart the random sequence:
*
*rGen.init( rgen.getSeed() )// Will restart system  
with original seed
*rGen.init()// Will restart system  
with new seed

*
*random()
*Shortcut method to randomReal48()
*
*randomReal48()
*Returns a random 48-bit floating point number on interval  
[0,1).
*Note that this currently will not work properly in Flash due  
to the limitations
*of the number type. Results are with only 32-bit precision.  
(verify this?)

*
**/

class com.jbradley.math.LCG {

/* Period parameters */
static var M = Math.pow(2.8, 14);   /* Period of LCG */
static var A = 2862933555777941757; /* 48-bit Mersenne Prime */
static var C = 3037000493;  /* 16-bit prime? */

var time:Number = 0;
var seed:Number = 0;

f