Re: [Flashcoders] Producing a random list with no repeats

2010-05-09 Thread Juan Pablo Califano
If that happens, it's a bug in the player. From the docs:

"Returns a pseudo-random number n, where 0 <= n < 1. "

So, the biggest number random returns can approximate 1, but should never be
1.

What I got wrong in my original answer was that multiplying by 2 and then
rounding introduces a bias towards  1 (and then if you substract 1, the bias
is towards 0).

Think about it this way:

You've got a number between 0 and 1.9 when you do Math.random *  2. That
is, a number that can get very close to 2, but never be actually 2.

If you round it you'll get:

0 for 0...0.49
1 for 0.5...1.49
2 for 1.5...19

The actual numbers might not be exact as I'm not sure what's the boundary
for rounding up or down; I'm also considering only two decimals for
simplicity.

But anyway, the distribution is obviously not even. There's about a 0.25
chance of getting 0, the same chance of getting 2, but the double, 0.5, of
getting 1.

However, if you multiply by 3 and floor the result (or coerce it to int for
that matter), you get rid of the bias:

Math.random * 3 will give a number in the range 0...2.99

So, if you get rid of the decimal part, you have:

0 for 0...0.99
1 for 1...1.99
2 for 2...2.99

Which gives a result that's clearly more evenly distributed than the first
approach.


2010/5/9 Steven Sacks 

> In the exception that Math.random() returns 1, in which case you would get
> 2.
>
> I don't use Math.random(), though, I use the Parker-Miller PRNG.
>
>
>
>
> On 5/9/2010 5:01 PM, Juan Pablo Califano wrote:
>
>> PS 2: I think the correct way to write the function that returns a number
>> in
>> the range -1 / 1 is:
>>
>> Math.floor(Math.random * 3) - 1
>>
>> In this case, you could indeed use int() instead of a static Math method,
>> so
>> I guess I was wrong about your function needing a Math.round call.
>>
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Producing a random list with no repeats

2010-05-09 Thread Steven Sacks

In the exception that Math.random() returns 1, in which case you would get 2.

I don't use Math.random(), though, I use the Parker-Miller PRNG.



On 5/9/2010 5:01 PM, Juan Pablo Califano wrote:

PS 2: I think the correct way to write the function that returns a number in
the range -1 / 1 is:

Math.floor(Math.random * 3) - 1

In this case, you could indeed use int() instead of a static Math method, so
I guess I was wrong about your function needing a Math.round call.

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Producing a random list with no repeats

2010-05-09 Thread Juan Pablo Califano
PS 2: I think the correct way to write the function that returns a number in
the range -1 / 1 is:

Math.floor(Math.random * 3) - 1

In this case, you could indeed use int() instead of a static Math method, so
I guess I was wrong about your function needing a Math.round call.

2010/5/9 Juan Pablo Califano 

> Steven, sorry to contradict you, but although your code is simple and sort
> of works, there a couple of problems with it.
>
> First, the sorting function is wrong. A sorting function is supposed to
> return -1,0 or 1. But it only returns -1 or 0.
>
> The problem is you are coercing the result to int. You should use
> Math.round instead.
>
> int(Math.random() * 2) will return 1 or 0, never 2, because the biggest
> value Math.random will return will be close to 1, but never will be 1. So,
> if Math.random returns say, 0.9, after you multiply you get 1.8; since
> you're using int(), decimals will just be discarded (instead of rounded) and
> the result will be 1. There's no way you'll get 2, only 1 or 0. Then you
> substract 1, so your sorting function will return either -1 or 0.
>
> Second, you don't need to sort the whole list. It's much more work than
> it's needed to shuffle the array. While the Fisher-Yates algorithm is
> linear, the sort is not. That is, if you have 40 items, you know the
> Fisher-Yates will "visit" each list slot only once. That's not the case with
> the sort method. It grows exponentially. Well, maybe not exactly
> exponential, I'm not possitive, but it's not linear anyway, as this code
> shows:
>
> var list:Array = [];
> var numItems:int = 40;
> for(var i:int = 0; i < numItems; i++) {
> list[i] = i;
> }
>
> var calls:int = 0;
> function randomize(a:int, b:int):int
> {
> calls++;
> return int(Math.random() * 2) - 1;
> }
>
> list.sort(randomize);
> trace("calls:" + calls);
>
> Change numItems and you'll see what I mean. Bottom line, you're doing more
> work than you need: you're calling a function instead of doing your
> comparison inline and you are calling Math.random more than it's actually
> needed to sort the list.
>
> Third, the sorting algorithm works under the assumption that given two
> objects they allways sort the same: either the first one is less than, equal
> or greater than the second. That doesn't hold true if your sorting function
> returns a random result. And I think maybe that's why the number of calls
> that the above code prints vary even when you have the same number of items.
>
> Cheers
> Juan Pablo Califano
>
> PS. I also used the method you describe (for years) until I read about its
> problems, so I thought maybe this is a good opportunity to pass this info
> on. To be honest, though, I don't think it will be a problem
> preformance-wise unless you have a rather big array.
>
> 2010/5/9 Steven Sacks 
>
> Here's a very clean, fast and simple way to randomize an array.
>>
>> array.sort(randomizeFunction);
>>
>> function randomize(a:int, b:int):int
>> {
>>return int(Math.random() * 2) - 1;
>> }
>>
>> If you're not happy with Math.random()'s randomness, there are plenty of
>> other random number generators out there, such as:
>>
>>
>> http://lab.polygonal.de/2007/04/21/a-good-pseudo-random-number-generator-prng/
>>
>> And Grant Skinner's Rndm:
>> http://www.gskinner.com/blog/archives/2008/01/source_code_see.html
>>
>> ___
>> Flashcoders mailing list
>> Flashcoders@chattyfig.figleaf.com
>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>
>
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Producing a random list with no repeats

2010-05-09 Thread Steven Sacks

You're right. I was careless on two counts.

Fisher-Yates it is.


On 5/9/2010 4:36 PM, Juan Pablo Califano wrote:

Steven, sorry to contradict you, but although your code is simple and sort
of works, there a couple of problems with it.

First, the sorting function is wrong. A sorting function is supposed to
return -1,0 or 1. But it only returns -1 or 0.

The problem is you are coercing the result to int. You should use Math.round
instead.

int(Math.random() * 2) will return 1 or 0, never 2, because the biggest
value Math.random will return will be close to 1, but never will be 1. So,
if Math.random returns say, 0.9, after you multiply you get 1.8; since
you're using int(), decimals will just be discarded (instead of rounded) and
the result will be 1. There's no way you'll get 2, only 1 or 0. Then you
substract 1, so your sorting function will return either -1 or 0.

Second, you don't need to sort the whole list. It's much more work than it's
needed to shuffle the array. While the Fisher-Yates algorithm is linear, the
sort is not. That is, if you have 40 items, you know the Fisher-Yates will
"visit" each list slot only once. That's not the case with the sort method.
It grows exponentially. Well, maybe not exactly exponential, I'm not
possitive, but it's not linear anyway, as this code shows:

var list:Array = [];
var numItems:int = 40;
for(var i:int = 0; i<  numItems; i++) {
list[i] = i;
}

var calls:int = 0;
function randomize(a:int, b:int):int
{
calls++;
return int(Math.random() * 2) - 1;
}

list.sort(randomize);
trace("calls:" + calls);

Change numItems and you'll see what I mean. Bottom line, you're doing more
work than you need: you're calling a function instead of doing your
comparison inline and you are calling Math.random more than it's actually
needed to sort the list.

Third, the sorting algorithm works under the assumption that given two
objects they allways sort the same: either the first one is less than, equal
or greater than the second. That doesn't hold true if your sorting function
returns a random result. And I think maybe that's why the number of calls
that the above code prints vary even when you have the same number of items.

Cheers
Juan Pablo Califano

PS. I also used the method you describe (for years) until I read about its
problems, so I thought maybe this is a good opportunity to pass this info
on. To be honest, though, I don't think it will be a problem
preformance-wise unless you have a rather big array.

2010/5/9 Steven Sacks


Here's a very clean, fast and simple way to randomize an array.

array.sort(randomizeFunction);

function randomize(a:int, b:int):int
{
return int(Math.random() * 2) - 1;
}

If you're not happy with Math.random()'s randomness, there are plenty of
other random number generators out there, such as:


http://lab.polygonal.de/2007/04/21/a-good-pseudo-random-number-generator-prng/

And Grant Skinner's Rndm:
http://www.gskinner.com/blog/archives/2008/01/source_code_see.html

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Producing a random list with no repeats

2010-05-09 Thread Juan Pablo Califano
Steven, sorry to contradict you, but although your code is simple and sort
of works, there a couple of problems with it.

First, the sorting function is wrong. A sorting function is supposed to
return -1,0 or 1. But it only returns -1 or 0.

The problem is you are coercing the result to int. You should use Math.round
instead.

int(Math.random() * 2) will return 1 or 0, never 2, because the biggest
value Math.random will return will be close to 1, but never will be 1. So,
if Math.random returns say, 0.9, after you multiply you get 1.8; since
you're using int(), decimals will just be discarded (instead of rounded) and
the result will be 1. There's no way you'll get 2, only 1 or 0. Then you
substract 1, so your sorting function will return either -1 or 0.

Second, you don't need to sort the whole list. It's much more work than it's
needed to shuffle the array. While the Fisher-Yates algorithm is linear, the
sort is not. That is, if you have 40 items, you know the Fisher-Yates will
"visit" each list slot only once. That's not the case with the sort method.
It grows exponentially. Well, maybe not exactly exponential, I'm not
possitive, but it's not linear anyway, as this code shows:

var list:Array = [];
var numItems:int = 40;
for(var i:int = 0; i < numItems; i++) {
list[i] = i;
}

var calls:int = 0;
function randomize(a:int, b:int):int
{
calls++;
return int(Math.random() * 2) - 1;
}

list.sort(randomize);
trace("calls:" + calls);

Change numItems and you'll see what I mean. Bottom line, you're doing more
work than you need: you're calling a function instead of doing your
comparison inline and you are calling Math.random more than it's actually
needed to sort the list.

Third, the sorting algorithm works under the assumption that given two
objects they allways sort the same: either the first one is less than, equal
or greater than the second. That doesn't hold true if your sorting function
returns a random result. And I think maybe that's why the number of calls
that the above code prints vary even when you have the same number of items.

Cheers
Juan Pablo Califano

PS. I also used the method you describe (for years) until I read about its
problems, so I thought maybe this is a good opportunity to pass this info
on. To be honest, though, I don't think it will be a problem
preformance-wise unless you have a rather big array.

2010/5/9 Steven Sacks 

> Here's a very clean, fast and simple way to randomize an array.
>
> array.sort(randomizeFunction);
>
> function randomize(a:int, b:int):int
> {
>return int(Math.random() * 2) - 1;
> }
>
> If you're not happy with Math.random()'s randomness, there are plenty of
> other random number generators out there, such as:
>
>
> http://lab.polygonal.de/2007/04/21/a-good-pseudo-random-number-generator-prng/
>
> And Grant Skinner's Rndm:
> http://www.gskinner.com/blog/archives/2008/01/source_code_see.html
>
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Producing a random list with no repeats

2010-05-09 Thread Steven Sacks

Here's a very clean, fast and simple way to randomize an array.

array.sort(randomizeFunction);

function randomize(a:int, b:int):int
{
return int(Math.random() * 2) - 1;
}

If you're not happy with Math.random()'s randomness, there are plenty of other 
random number generators out there, such as:


http://lab.polygonal.de/2007/04/21/a-good-pseudo-random-number-generator-prng/

And Grant Skinner's Rndm:
http://www.gskinner.com/blog/archives/2008/01/source_code_see.html
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


[Flashcoders] Re: Re: Producing a random list with no repeats

2010-05-09 Thread Alan Neilsen
Thanks to all the suggestions re my random list problem. Particular thanks to 
Gerry Beauregard for this suggestion:

"It's probably best just to create an Array or Vector containing numbers 1 to 
40.  (Vectors are only available in AS3, not sure about Array).  Shuffle the 
array, then choose the first 10 elements.."

The following method of shuffling an array works fine in AS2:

var numArray:Array = new Array();
numArray = 
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40];
shuffle(numArray);

function shuffle(arr:Array) {
 var i:Number;
 for (i in arr) {
   var tmp = arr[i];
   var rNr:Number = Math.floor(Math.random()*arr.length);
   arr[i] = arr[rNr];
   arr[rNr] = tmp;
 }
}


This message is for the named person’s use only. It may contain
confidential, proprietary or legally privileged information. No
confidentiality or privilege is waived or; lost by any mistransmission. If
you receive this message in error, please immediately delete it and all
copies of it from your system, destroy any hard copies of it and notify
the sender. You must not directly or indirectly, use, disclose,
distribute, print or copy any part of this message if you are not the
intended recipient. GOULBURN OVENS INSTITUTE OF TAFE and
any of its subsidiaries each reserve the right to monitor all e-mail
communications through its networks. Any views expressed in this
message are those of the individual sender, except where the
message states otherwise and the sender is authorised to state them
to be the views of any such entity.

#
This e-mail message has been scanned for Viruses and Content and cleared 
by MailMarshal
#
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Apple developing flash-like alternative

2010-05-09 Thread Dave Watts
> Well, you have a point Paul.  But compare and contrast to Microsoft.  
> Microsoft liked
> the Flash client too, so they came up with their own.  They never went on an 
> insult
> spree.  Apple apparently likes the Flash client too, but they did go on an 
> insult spree.
> That's underhanded to me, and in my personal opinion, not good business form.

I'm not sure that Gianduja is intended as an analogue to Flash. It
looks more like another JavaScript library to me, and there are plenty
of those already.

> Yes, of course I agree.  But I am concerned that our world is going to get 
> split in two
> now (not that it isn't fragmented already).  Isn't the concept for what we do 
> supposed to
> be write once, deploy everywhere?  Instead, we're going the route of one for 
> i* and one
> for Android/Flash.

Well, that's too bad, but there's nothing to be done about it. Apple
strictly controls their mobile device platform, and it's not in their
perceived best interest to support Flash. They see this as diluting
the value of their platform, and I think they're probably right about
this. They're gambling that people will continue developing for their
platform, and will develop first for their platform, which will be a
disadvantage for competing platforms like Android.

Of course, their gamble could be wrong. If you don't like it, I
suggest you not develop for the App Store. If enough people do this,
the problem will self-correct.

Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/
http://training.figleaf.com/

Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on
GSA Schedule, and provides the highest caliber vendor-authorized
instruction at our training centers, online, or onsite.

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] Apple developing flash-like alternative

2010-05-09 Thread Mendelsohn, Michael
Well, you have a point Paul.  But compare and contrast to Microsoft.  Microsoft 
liked the Flash client too, so they came up with their own.  They never went on 
an insult spree.  Apple apparently likes the Flash client too, but they did go 
on an insult spree.  That's underhanded to me, and in my personal opinion, not 
good business form.

> I'm sure that Adobe is looking very hard now at the strength and weaknesses 
> of the flash platform and we will all benefit in the long term from Adobe 
> being given a kick.

Yes, of course I agree.  But I am concerned that our world is going to get 
split in two now (not that it isn't fragmented already).  Isn't the concept for 
what we do supposed to be write once, deploy everywhere?  Instead, we're going 
the route of one for i* and one for Android/Flash.

- MM



___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Apple developing flash-like alternative

2010-05-09 Thread Paul Andrews

On 09/05/2010 02:55, Mendelsohn, Michael wrote:

I'd rather not go negative, especially on this list which is so valuable to me, 
but this Flash Alternative news simply disgusts me.  It's as underhanded as a 
business can get.
   


Why would any business reveal their intentions before they are ready? I 
don't see why any company has to announce their intentions beforehand, 
or any reason why any company shouldn't build their own development 
system. Underhanded? Seems like good business practice to me.


As a Flash developer I'm not happy to be on the wrong side of this 
strategy, but I can't see that it's underhand. Just business practice.


Ultimately  Apple is doing us all a big favour. I'm sure that Adobe is 
looking very hard now at the strength and weaknesses of the flash 
platform and we will all benefit in the long term from Adobe being given 
a kick. Personally I have been completely underwhelmed in CS4 and I 
can't really get excited about CS5, except perhaps as a more coherent 
build platform for Flash Builder.




- MM

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

   


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders