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,

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

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

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

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:

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

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

2010-05-07 Thread Hans Wichman
Hi, here an alternative way of producing a random array: /** * Randomizes a copy of this array. */ public function randomize (pArray:Array):Array { var newArray:Array = pArray.slice(); var i:Number = newArray.length; while (i) { var p:Number = random(i); var t:Object =

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

2010-05-07 Thread Hans Wichman
ah my bad , i think this is probably the same as fisher yates;) On Fri, May 7, 2010 at 12:20 PM, Hans Wichman j.c.wich...@objectpainters.com wrote: Hi, here an alternative way of producing a random array: /** * Randomizes a copy of this array. */ public function randomize

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

2010-05-06 Thread kennethkawam...@gmail.com
I always use Fisher-Yates shuffle method to randomise an Array, which yields more unbiased result. http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle My AS3 interpretation of Fisher-Yates is as follows; I can just call this from anywhere in my scripts ;) package utils { public

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

2010-05-06 Thread Merrill, Jason
...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of kennethkawam...@gmail.com Sent: Thursday, May 06, 2010 5:03 AM To: Flash Coders List Subject: Re: [Flashcoders] Producing a random list with no repeats I always use Fisher-Yates shuffle method to randomise an Array, which yields

[Flashcoders] Producing a random list with no repeats

2010-05-05 Thread Alan Neilsen
I am working in ActionScript 2. I want to create a quiz in which 10 questions are randomly selected from a block of 40 questions. I found the following code, but I can't work out how to stop it doubling up the questions. function randRange(min:Number, max:Number):Number { var

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

2010-05-05 Thread Gerry Beauregard
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. A reasonably effective way to shuffle an array is to step through the array, and for each element

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

2010-05-05 Thread Karl DeSaulniers
Hi Allen, Just a thought, but maybe have the quiz numbers set in an array that the var n:Number is retrieved and have the min and max that the random code calls match the amount of items in the array. After selecting a question, remove it from the array, thus the array length (max) reduces

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

2010-05-05 Thread Juan Pablo Califano
A simple way: Put all the candidate numbers in a list (in this case, 1 to 40). Then pick randomly from that array, one at the time, and make sure you remove that number from the candidates list, so you can't have duplicates. In code (untested): function getRandomList():Array { var