Forum Topic: AS3: old-fashioned random

(1,389 views • 7 replies)

This topic is 1 page long.

<< < > >>
None

LesPaulPlayer

Reply To Post Reply & Quote

Posted at: 5/6/07 04:09 PM

LesPaulPlayer DARK LEVEL 12

Sign-Up: 05/18/06

Posts: 673

niaM:3SA

Ok, so typing out Math.round(Math.random()*10) is quite annoying when you want to get a random whole number between 1 and 10. Here is a super-crazy complicated class I wrote in about 10 hours that will make it a lot easier:

package {
public class randomFunction {
public function randomFunction() {
}
public function random(integer:int):int {
return Math.floor(Math.random() * integer);
}
}
}

save that in the same directory of your game .fla file and use it like so:

var Math:randomFunction = new randomFunction()

and then, when you want to get the random number:

trace(Math.random(10))


None

TastyLamp

Reply To Post Reply & Quote

Posted at: 5/6/07 06:08 PM

TastyLamp LIGHT LEVEL 11

Sign-Up: 07/13/06

Posts: 224

Howabout this one. Save it as "Math2.as".

package{
public class Math2 extends Math{
public static function random2(min:Number, max:Number):Number{
return Math.round(Math.random()*(max-min)+min);
}
}
}

Then use it like this:

var number1 = Math2.rand(5, 10); //5 is the minimum, 10 is the max
var number2 = Math2.rand(5, 10);
var number = Math2.max(number1, number2); //this gets the highest number


None

Denvish

Reply To Post Reply & Quote

Posted at: 5/6/07 06:19 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,229

Or if you want to go down the procedural route, chuck this function somewhere in your fla code:

function Random(nm:int):int{
return Math.floor(Math.random()*nm);
}

Then call it in a very similar fashion to the good ol' random(x), only with a capital letter to avoid conflicts:

var xxx:Number=Random(4); //WILL RETURN 0, 1, 2 or 3
trace(xxx);

- - Flash - Music - Images - -

BBS Signature

None

LesPaulPlayer

Reply To Post Reply & Quote

Posted at: 5/6/07 06:42 PM

LesPaulPlayer DARK LEVEL 12

Sign-Up: 05/18/06

Posts: 673

Denvish,
if you do it your way, its not exactly like AS2. Also, random(number) is a differenct function than just random(). The functions are sensitive to whether you have input or not, so no need to give it a new name.


None

Denvish

Reply To Post Reply & Quote

Posted at: 5/6/07 07:29 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,229

At 5/6/07 06:42 PM, LesPaulPlayer wrote: Denvish,
if you do it your way, its not exactly like AS2.

How so?

Also, random(number) is a differenct function than just random().

Erm, yeah, I'm going for the former, wasn't even aware the latter existed

The functions are sensitive to whether you have input or not, so no need to give it a new name.

Fair enough, still prefer to play it safe

- - Flash - Music - Images - -

BBS Signature

None

Pyromaniac

Reply To Post Reply & Quote

Posted at: 5/6/07 07:31 PM

Pyromaniac EVIL LEVEL 18

Sign-Up: 01/14/05

Posts: 2,976

Math.random()*10 still returns intergers to me. I dont get any decimals. I really dont see the problem with it.


None

FingerFactor

Reply To Post Reply & Quote

Posted at: 9/20/08 08:51 AM

FingerFactor LIGHT LEVEL 14

Sign-Up: 12/01/07

Posts: 79

Then your allocating the value to an integer... (automatically truncates)

Anyways... just a useful tip: Use int's whenever possible, for instance if your storing the position of a particle or some other onscreen element. In many cases they will noticeably speed up the execution of your code a noticeable amount because they are less taxing on the memory and cpu.


None

FingerFactor

Reply To Post Reply & Quote

Posted at: 9/20/08 08:53 AM

FingerFactor LIGHT LEVEL 14

Sign-Up: 12/01/07

Posts: 79

At 9/20/08 08:51 AM, FingerFactor wrote: In many cases they will speed up the execution of your code a noticeable amount because they are less taxing on the memory and cpu.

Im sry, typos are the devil and I saw it and.. bleh :S


All times are Eastern Standard Time (GMT -5) | Current Time: 11:46 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!