I need a suggestion. I want to have a function that returns random numbers from let say 1 to 100, with condition to not repeat the chosen number. It is something like chess table that will be filled with something random and not one thing over another thing... If someone can tell a suggestion I'll be very happy. Thanks.
2 Answers
Create an Array of 100 numbers (1..100), then 'sort' the Array by 'random'. You can then pull out the numbers one at a time working your way through the array.
I haven't tested the code below but I had these snippets available that you could piece together to achieve the intended result.
public static function randomNumber(min:Number, max:Number):Number{
var rnd:Number = Math.floor((Math.random()*((max+1)-min))+min);
return rnd;
}
public static function randomize(arr:Array):Array{
var len:Number = arr.length;
var rnd:Number;
var tmp:Object;
for(var i:Number=0;i<len;i++){
rnd = randomNumber(0,(len-1));
tmp = arr[i];
arr[i] = arr[rnd];
arr[rnd] = tmp;
}
return arr;
}
var setOfNumbers:Array = new Array();
for(var i:int=0;i<100;i++){
setOfNumbers[i] = (i+1);
}
var shuffledSetOfNumbers:Array = randomize(setOfNumbers);
Notes:
- For the purists this "randomizing" isn't "truly" random (if you're writing a Card shuffler for a Vegas gambling machine you'll want to use something different - case in point!)
- My
randomNumberandrandomizefunctions above arestaticas I've typically included them that way in the apps I've needed them but you don't have to use it this way - My original lib used
Numbervsintoruintfor some of the variables for more options when used but feel free to clean that up
7 Comments
tziuka
It's work perfectly but I don't understand what are you doing in the randomize for. If you want to explain a little. Thank you.
tziuka
If you can explain how it's work with small array, let's say 5 items.
scunliffe
@tziuka the
randomize function takes an array of "things" (in your case numbers (but it could be users, items, cards, shapes, etc.). It determines the length of the array then loops over that length swapping a random item in the list for the current index. It isn't perfect but it will certainly mix up the items in the list.Bob
We used to do something very similiar, except we would add a "rdm" property to the object in the array, and then actually use Array.sort("rdm") Not sure if it's worth a new answer or not.
scunliffe
@Bob it's funny you mention that as I just checked and I have a previous answer that takes a similar approach: stackoverflow.com/questions/7913412/…
|
also like that...
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* @author Vadym Gordiienko
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var startArray:Array = generateNumberArray(100);
var randomArray:Array = randomArray(startArray);
trace("startArray = " + startArray);
trace("randomArray = " + randomArray);
}
/**
* generate Array of numbers by length
* @param length
* @return Array of numbers
*/
public static function generateNumberArray(length:int):Array
{
var numberArray:Array = [];
for (var i:int = 0; i < length; i++)
{
numberArray[i] = i+1;
}
return numberArray;
}
/**
* generate randomly mixed array by input array
* @param inputArray - simple not mixed array
* @return Array - mixed array
*/
public static function randomArray(inputArray:Array):Array
{
var randomArray:Array = [];
var tempArray:Array = [];
for (var i:int = 0; i < inputArray.length; i++)
{
tempArray.push(inputArray[i]);
}
while (tempArray.length)
{
var randomNumber:int = Math.round(Math.random() * (tempArray.length - 1));// get random number of left array
randomArray.push( tempArray[randomNumber] );
tempArray.splice(randomNumber, 1); // remove randomed element from temporary aarray
}
tempArray = null;
delete [tempArray];
return randomArray;
}
}
}