Random Numbers AS2
- Started
- Last post
- 14 Responses
- Cenitgrade
Hey,
Anyone know how to force AS2 to generate random numbers that have a minimum change between them.
for example if I have a 0-100 range and the number generated is 50. The next number generated cannot be within 10 of this number.
Cheers
Lads and lassies
- kumori040
You would have to record the generated number before running the function again. If the inline var generates number within the range you don't want, just have it run the function again from the start.
- Cenitgrade0
Ah yes... how do I record the number.... Some things in flash just baffle me!
I tried this but of course it didn't work....
//range from 0 to 60 wtih 0 as minimum
randomY = Math.floor(Math.random() * (60 - 00)) +00;
if(randomY - randomY <5){
randomY = randomY +5;
}
- fyoucher10
var currentNum:Number = 0;
///////////////
// Generic random number script
// to select a random number
// between two numbers
///////////////
function getRandomNumber
( min:Number, max:Number )
:Number
{
if (min > max) {
var temp:Number = min;
min = max;
max = temp;
}
return ( Math.floor ( Math.random() * ( max - min + 1 )) + min );
}///////////////
// Gets a random number
// witha difference of 10
// from the last number
///////////////
function getRandomNumberRange
( min:Number, max:Number )
{//Number's we want to mess with
var num:Number = getRandomNumber( min, max );
//Final output is offset by numDiff
var numDiff:Number = 10;
//Make sure num isn't within numDiff +/-
if (
( num <= currentNum + numDiff )
||
( num >= currentNum - numDiff )
){
//Difference randomized positive or negative???
var negOrPos:Number = getRandomNumber(1,2);
if (negOrPos == 1) { numDiff = numDiff * -1 }
num = num += numDiff;
}currentNum = num;
trace(num);
return (num);
}///////////////
//Sample usage. A button could call this...
///////////////
getRandomNumberRange
( Math.round( Math.random()*100 ),
Math.round ( Math.random()*100 ) );
- amullins0
y not just split the range into 2 separate ranges after the first random # has been chosen.
range 0-100
first random # is 50
ranges [0-40], [60-100]
randomly choose a range and then randomly choose a # within that range.
- Cenitgrade0
Cheers people.
I'll have a tinker now and see how I get on.
- must_dash0
There is a danger of spliting it into two seperate ranges, as I'd imagine you'd end up tending towards either 0 or 100... e.g if you had a random number of 20, and then had to go either side, you'd need to weight which side it chose, otherwise it would be 50/50 of picking a number in a very small range, and then the next time it went around it would be even worse.
- kumori040
var myRandNum:Number = -1;
function findRandNum():Void {
var tempNum:Number = Math.floor(Math.max(0, Math.min(Math.random() * 100, 100)));if (tempNum >= (myRandNum - 10) && tempNum <= (myRandNum + 10)) {
trace("too close!");
findRandNum();
} else {
myRandNum = tempNum;
trace(myRandNum);
}
}- The first part of the function generates a random num between 0 - 100. The second determines if its 10 numbers too close to the first. Try it.kumori04
- kumori040
oops. forgot to call the function
findRandNum();
- Cenitgrade0
kumori, that piece of code works perfectly. Cheers. Sainthood is coming your way.
- must_dash0
I love maths
- must_dash0
Better than math.
- CyBrain0
additional good news: The Math class managed to be one of the few unscathed things in AS3.
- Cenitgrade0
AS2 to AS3 I have made the leap yet. Is it worth it or should I hold out for AS4?
- kumori040
Learn AS3. It is worth its weight in gold.
- Maybe I'll start with a few experiments as see how that goes.Cenitgrade