I suppose many of you know how to use sort to get random array…
For those who are not familiar with those nice feature of sort method I will explain the idea. Sort method can randomize array if you put function that return -1, 0 or 1 randomly, which basiclly tells sort randomly are elements less then, equal or greater then eash other.
AS2 Random Array based on Sort source:
var a:Array = new Array(“a”, “b”, “c”, “d”, “e”);
function shuffle(a,b):Number {
var num : Number = Math.round(Math.random()*2)-1;
return num;
}
var b:Array = a.sort(shuffle);
trace(b);
AS2 Random Array based on Sort source:
var a:Array = new Array(“a”, “b”, “c”, “d”, “e”);
function shuffle(a,b):int {
var num : int = Math.round(Math.random()*2)-1;
return num;
}
var b:Array = a.sort(shuffle);
trace(b);
EDIT: you can take a look at algo comparison, with source for much faster approach in new post
i tried your random adn it works great but i am trying to apply this to a multidimensional array and it doesn’t work. any suggestions?
how exactly you would like to sort a multidimensional array?
give me an example and I will try to provide an example
also I will soon post much more faster array sort method
well i ama creating a map out of a 2D array. Rows and columns like a checker board. the catch is it is simulating a 3D griad with walls and floor tiles. I gave each a numerical value of 100= floor and 200= wall. This is for a collision detection. If 200> cant go past. I want to keep the outer walls at 200 for a borader so all of row1 and lastrow will be 200 and the begining index and last index of every array in between will = 200. All the remaining indexes i want to randomize with the value of 100 or 200. Essentially creating a random maze.
well, seems like this function won’t do it for you
this is for randomizing an array (indexes of array) so
a,b,c,d becomes b,c,a,d or d,c,a,b
for your idea if I understood it good following lines could help
for (var i:Number;i
stupid comments
can’t show AS
link to text file
http://www.hagane-studio.com/as/maze/maze.txt
I tried using your nested loop and tweeked it a bit. Now i can’t get the if statement to work. i keep getting a value of 200 in places i don’t want. Any ideas why?
thanks
// 2D array
var gridsize:Number = 10;
// creating the map
var map_ray:Array = new Array(gridsize);
var i:Number;
var j:Number;
//incrementing the col to = gridsize
for (i=0; i
heh,
it’s not good writing a code from head
I made few mistakes
here are the examples
http://www.hagane-studio.com/as/maze/maze.swf
http://www.hagane-studio.com/as/maze/maze.fla
I hope this solves it out
Sorry, but your array randomisation routine has problems. Using sort will not randomise the array in a complete manner, much like giving a pack of cards one shuffle won’t sufficiently randomise them to prevent long runs of cards.
The following link explains:
http://www.ptolemy.co.uk/archives/as3/7
Pingback: Randomize Array - Shuffle an array in Flash « Cyberspace Nova
don’t be sorry
looking at your article and as I knowing math good I can agree it’s not ultimate array shuffle, but it’s good for beginners
stating it won’t shuffle pack of cards good is not true
pack of cards are shuffle good and good enough, taking it in long runs can lead to some cards show more times on one then another, that is true
your splice algorithm lacks on speed, I made another article which I found time to do know after you made your point ;
https://mrsteel.wordpress.com/2007/06/15/randomize-array-shuffle-an-array-in-flash/
Pingback: Array Aleatorios en AS2 » unijimpe
Pingback: Array Aleatorios en AS2 «
nice work man 10x
Nice trick, thanx.
wow!!! just now.
With your code I gave a second look to the Array.sort() method, and finally I can make sense to issues I had given up on.
One never stops learning, even about things you thought you knew well.
It’s a very efficient way to shuffle an array.
Thanks a lot, I was looking for a way less elegant solution, this is great.
Using “while” instead of “for”, as in the following function, reduces the processing time from 4 ms to 1 ms! So the stats are (Methods 1-4 are original, Method 5 is the while loop):
Method 1, time: 718
Method 2, time: 1957
Method 3, time: 3
Method 4, time: 4
Method 5 (while), time: 1
/////////////////////////
function mixArray(array:Array):Array {
var _length:Number = array.length, mixed:Array = array.slice(), rn:Number, it:Number, el:Object;
while (it < _length) {
el = mixed[it];
mixed[it] = mixed[rn = it + random(_length – it)];
mixed[rn] = el;
it++;
}
return mixed;
}
Amazing sir. This is by far one of the best solutions I’ve found yet.
Thanks alot, really appreciate you providing this for everyone around you.
Pingback: 2010 in review « Cyberspace Nova
As Alec McEachran seems to explain (although his link is broken), this function is slick and fast, but it’s not good if you want an even randomization. What I call an even randomization is for each item, to have a probability of 1/N to be found at any place in the randomized array.
So if you’re going to call this function several times and want each result as different as possible from the previous, you should consider the splice method instead.
More explanation can be found here :
http://snipplr.com/view/49292/beware-with-array-shuffle-function/
great tutorial, it’s help me to make a random map on my game
I think the admin of this web page is actually working
hard in favor of his web page, as here every data is quality based information.
Why users still use to read news papers when in this technological globe the whole thing is presented on net?
I am truly glad to read this web site posts which contains tons of valuable facts, thanks for providing these kinds of data.
Hi my loved one! I wish to say that this post is awesome, nice written and come with approximately
all significant infos. I’d like to peer extra posts like this .
Good day! This is my first comment here so I just wanted to give a quick
shout out and tell you I truly enjoy reading through your posts.
Can you suggest any other blogs/websites/forums that go over the
same topics? Thanks a ton!
When I originally commented I clicked the “Notify me when new comments are added” checkbox and
now each time a comment is added I get four e-mails with the same comment.
Is there any way you can remove people from that service?
Many thanks!
Hey can you help me to create 2 elements randomize array or pick two indexed array and put it randomly
so it should be like this
before using random function [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
after using random function [ 5, 6, 1, 2, 9, 10, 3, 4, 7, 8 ] or [9,10,1,2,7,8,3,4,5,6]
it always produce 1 before 2, 3 before 4, 5 before 6 and so on. Then put it randomly
it will be impossible to produce 2,1 or 10,9 or 6,5 and so on,
it will random 2 elements, not 1 by 1 but 2 by 2. Sorry for my bad English. I hope you understand what i mean.
hi wonder could anyone shed some light on how to create an array with ten audio questions picked randomly (add child from library) and using the left arrow, up arrow, right arrow to pick the answer from the A B or C options given, i also want the text questions displayed on stage, so i presume the random audio question would have to be selected first and when selected a timer will kick in some three seconds later to display the identical question, so as the child could see … the answers to the text questions will also be in an array like such…
var audioArray:Array = new Array [“quest1.wav”,”quest2.wav”,”quest3.wav”,”quest4.wav”,”quest5.wav”,”quest5.wav”,”quest7″,”quest8″,”quest9″,”quest10″];
var voice:String = audioArray[Math.floor(Math.random() * (audioArray.length – 1))];
trace(audioArray);
var newArray:Array = new Array();
function randomizeArray(array:Array):Array
{
var newArray:Array = new Array();
while (array.length > 0) // for ( var o:int = 0; o <20; o++)
{
newArray.push(array.splice(Math.floor(Math.random()*array.length), 1));
}
return newArray;
}
var myQuest:Array = new quest();
trace(myArray[0]);
trace(myQuest);
I also need addChild(); to bring the audio from the library
var questions:Array =["Where is Dublin? ","What is gold? ","Name the Cat species? ","What is the capital of Peru?","What date does Christmas day fall on? ","State the colour Of Daffodils? ","What are the initials for America ","How many fingers has a bird","What do you boil to make tea with","Where does families usually go in Summer "];
var answers:Array=[ ["Ireland","Wales","Scotland"], ["Fabric","Metal","Plastic"], ["Bovine","Equine","Feline"] ,[ "Lima","Arizona","Cologne"] ,["1st April","25th December","30th June"], ["Yellow","Red","Purple"], ["AMC","MER","USA"] ,[ "two","none","three"] ,
["Water","Milk","Olive Oil"], ["To bed","To Work","On Holiday"], ];
I am putting the answers in such a way to be able to correspond with the question
if(rnd1==2){opt1.text=answers[rnd2][2];opt2.text=answers[rnd2][0];opt3.text=answers[rnd2][1];
(So the first set of brackets will always display where the correct answer lies)
Finally, each single question must be answered correctly before the program moves on to the next question PLUS I want only three questions to be chosen in the game each time.
I really hope someone can give me some insight into how i proceed, i have tried several combinations, but so far to no avail