Generate random number from specific set of numbers as3 - actionscript-3

I would like to create a random number between specific numbers,
let say I have the numbers 1,2,4,5,7,8,9 and I want to generate a number between these specific numbers. Is it possible?
pay attention that the number 3 and the number 6 should not be in the generated numbers. Only one of the written numbers should be in the equation.

What about:
var arr:Array = [ 1,2,4,5,7,8,9 ];
var rand:Number = arr[ Math.floor( Math.random() * arr.length ) ];
trace( rand );
Or even more elegant:
var rand:Number = arr[ ( Math.random() * arr.length ) | 0 ];

Related

Loop through random numbers

I'm trying to spread 8 balls on the stage with an uneven space between them within a range. using myNum variable in this statement ball.x = 150 + i * myNum; inside for loop I was hopping that it will spread each of the 8 balls in an uneven space. However this is not what's happening, instead it position 8 balls with an even space and then about a minute later it positions another set of 8 balls with different spacing.
var minLimit: int = 25;
var maxLimit: int = 43;
var range: int = maxLimit - minLimit;
var myNum: Number = Math.ceil(Math.random() * range) + minLimit;
var balls: Array = [],
ball: bomb30a;
for (var i: int = 0; i < 8; i++) {
ball = new bomb30a();
ball.x = 150 + i * myNum;
ball.y = 242;
balls.push(ball);
addChild(ball);
}
Your code defines a random number myNum, then loops 8 times. Why would myNum change in the middle of the loop?
If you want each iteration in the loop to use a different random number, you need to move the random number code into the loop so it gets executed on each iteration.
ball.x = 150 + i * (Math.ceil(Math.random() * range) + minLimit);

How can I make Math.random call a different value everytime it is called in AS3?

Let's say I had something like what's below in a function, the first time it gets called, I have a random value, but every other time it's called, I get the same number as it spit out the first time. What can I do to ensure it is randomized each time it is called?
if (Cosmo.hitTestObject(Asteroid5))
{
Asteroid5.y = (Math.random() * 20 - 5);
Asteroid5.x = (Math.random() * 20 - 15);
Asteroid5.x = Asteroid5.x + (Math.random() * 20 - 15);
Asteroid5.y = Asteroid5.y + (Math.random() * 20 - 5);
}
[Edited, based on comments below]
The AS3 reference page for Math.random() says:
Returns a pseudo-random number n, where 0 <= n < 1. The number
returned is calculated in an undisclosed manner, and is
"pseudo-random" because the calculation inevitably contains some
element of non-randomness.
If you want to use a seed-based pseudo-random number generator, this should help you:
http://www.kirupa.com/forum/showthread.php?365564-AS3-Seeded-Pseudo-Random-Number-Generator
in your 1st 2 statements, it looks like you're using Asteroid5.y & Asteroid5.x as variables. so one idea would be to use variables of the correct type (i'm assuming it's Number):
var rndX:Number = (Math.random() * 20 - 15);
var rndY:Number = (Math.random() * 20 - 5);
var rndX2:Number = rndX + (Math.random() * 20 - 15);
var rndY2:Number = rndY + (Math.random() * 20 - 5);
Asteroid5.x = rndX2;
Asteroid5.y = rndY2; or
var rndX:Number;
var rndY:Number;
for ( var i:uint=0; i<2; i++ )
{
rndX += (Math.random() * 20 - 15);
rndY += (Math.random() * 20 - 5);
}
Asteroid5.x = rndX;
Asteroid5.y = rndY;
now you can step through your code in debug mode and see exactly what going on as the values of the variables change. also maybe search your code for 'Asteroid5.x =' & 'Asteroid5.y =' & 'Asteroid5.y=' & 'Asteroid5.x='

Decimals to one decimal place in as3?

I randomly generate a decimal using:
private function randomNumber(min:Number, max:Number):Number
{
return Math.random() * (max - min) + min;
}
It comes out with something like 1.34235346435.
How can I convert it so that its 1.3.
You can round to one decimal place like this;
var newValue:Number = Math.round(oldValue * 10)/10
Or an arbitrary number of decimal places like this:
function round2(num:Number, decimals:int):Number
{
var m:int = Math.pow(10, decimals);
return Math.round(num * m) / m;
}
trace(round2(1.3231321321, 3)); //1.323
Just use the .toFixed or .toPrecision method, it doesn't have to be complicated (note that the number will become a string so you'll need to assign it to such or convert it back).
eg.
var numb:Number = 4.3265891;
var newnumb;
newnumb=numb.toFixed(2);//rounds to two decimal places, can be any number up to 20
trace(newnumb);//traces 4.33
newnumb=numb.toPrecision(3);//the 3 means round to the first 3 numbers, can be any number from 1 to 20
trace(newnumb);//traces 4.33
If you need Number as result and performance, I would say this solution is more efficient than the Math.pow()/Math.round() one. If you need 3 decimals just change 100 by 1000.
var myNumber:Number = 3.553366582;
myNumber = (( myNumber * 100 + 0.5) >> 0) / 100;
//trace = 3.55
demonstrating the rounding :
var myNumber:Number = 3.557366582;
myNumber = (( myNumber * 100 + 0.5) >> 0) / 100;
//trace = 3.56
use this instead
return Math.Round((Math.random() * (max - min) + min),1);
,1 will round up till 1 place
for 2 places of decimal you can use
return Math.Round((Math.random() * (max - min) + min),2);
I hope this helps now.

Generating random number from 0 to 10 including both

I heard of some Math.random() but I'm not sure about using it correctly. Number must be whole, so I suppose I'll need some rounding function.
Math.random produces a pseudo random number between [0;1[ (0 included, but 1 excluded)
To have an evenly distributed probability for all numbers between 0 and 10, you need to:
var a : int = Math.floor( Math.random() * 11 )
Math.random() returns a number between 0 - 1. Multiply it with 10 an round it using an int.
// random number between 1- 10
var randomRounded : int = Math.round(Math.random() * 10);
Edit: more accurate version
// more accurate
var randomRounded : int = Math.floor(Math.random() * 11);
Math.round(Math.random()*10); maybe?

increase chances of even number

If I am getting a random number, how do I increase my chances of making that random number to be even. I am not looking to make it even every time. I am just looking to generate a random number say... %70 of the time or %90 of the time.
private function randNum (high, low) {
return Math.floor(Math.random() * (1+high-low)) + low;
}
Would I pass in a greater range of numbers to this function? Or would I have to scrap this function altogether?
Thank you
private function randNum (high : Number, low : Number) : int
{
var even : Boolean = Math.random() < 0.7; //set probability of even number here
var rand : int = Math.floor(Math.random() * (1+high-low)) + low;
if (even)
while (rand % 2 != 0)
rand = Math.floor(Math.random() * (1+high-low)) + low;
else
while (rand % 2 != 1)
rand = Math.floor(Math.random() * (1+high-low)) + low;
return rand;
}
Test:
var even : int = 0;
var odd : int = 0;
for (var i : int = 0; i < 100000; i++)
{
var a : int = randNum(1, 20);
if (a % 2 == 0)
even++;
else
odd++;
}
trace(even, odd);
Output:
[trace] 69869 30131
A little too late ;) but another one with no loop and using bit masking operation :
ret & -2 will make your number even, then depending on the result of (Math.random() >= evenProbability) you set the lower bit to be 1 to give an odd number
function randomRange(low:int, high:int, evenProbability:Number = 0.5):int{
var ret:int = int( Math.random() * ( 1 + high - low ) ) + low
return ( ret & -2 ) | int( Math.random() >= evenProbability )
}
Here a live test with wonderfl : http://wonderfl.net/c/9IHx