Be a Supporter!

Check range within math.random?

  • 888 Views
  • 28 Replies
New Topic Respond to this Topic
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Check range within math.random? Feb. 2nd, 2014 @ 12:45 AM Reply

My code currently makes a random number between 1-3 and then based off which number it is the AI in my game executes one of three choices: attack, magic attack, heal.

function aiChoices():void
			{
				aiNumber = Math.ceil(Math.random() * 3);
				if (aiNumber == 1)
				{
					aiAttack();
				}
				if (aiNumber == 2)
				{
                                        aiMagic();
				}
				if (aiNumber == 3)
				{
					aiHeal();
				}
                   }

The code works perfectly. But I realized that I wanted to make it so that more often the AI attacks, less often uses magic, and even less often heals. To do this I changed my math.random to:

aiNumber = Math.ceil(Math.random() * 10);

I want to make him attack if the number is 1 - 7, magic if 8 - 9, and heal if 10. I don't know how to check if it is within a certain range. How would I change this:

if (aiNumber == 1)
				{
					aiAttack();
				}

To check if aiNumber is between 1 and 7?

Didn't know the exact words to Google and thus I just ended up getting webpages/forums of people not know how to generate a random number with math.random.

Thanks in advance guys.


BBS Signature
Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Check range within math.random? Feb. 2nd, 2014 @ 12:55 AM Reply

This should do the trick:

if (aiNumber <= 7)
{
	aiAttack();
}
else if (aiNumber <= 9)
{
	aiMagic();
}
else
{
	aiHeal();
}
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Response to Check range within math.random? Feb. 2nd, 2014 @ 01:00 AM Reply

At 2/2/14 12:55 AM, Etherblood wrote: This should do the trick:

if (aiNumber <= 7)
{
aiAttack();
}
else if (aiNumber <= 9)
{
aiMagic();
}
else
{
aiHeal();
}

Unfortunately that didn't work for the aiMagic because it would still cast magic if it was <= 9 but attack is <= 7. It works for attack and heal but I need magic to be 8 or 9 only. If it is <= 9 then odds are it will also be <= 7 which is what attack is.


BBS Signature
Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Check range within math.random? Feb. 2nd, 2014 @ 01:27 AM Reply

At 2/2/14 01:00 AM, Hero101 wrote:
Unfortunately that didn't work for the aiMagic because it would still cast magic if it was <= 9 but attack is <= 7. It works for attack and heal but I need magic to be 8 or 9 only. If it is <= 9 then odds are it will also be <= 7 which is what attack is.

Either I don't understand your question or I need to explain else-statements.
You want to call aiAttack if aiNumber is 1-7, aiMagic if it is 8-9 and aiHeal if it is 10?
Because this is what the code I posted does, if you want I can try to explain else-statements or you can just google it.

Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Response to Check range within math.random? Feb. 2nd, 2014 @ 01:31 AM Reply

At 2/2/14 01:27 AM, Etherblood wrote:
At 2/2/14 01:00 AM, Hero101 wrote:
Unfortunately that didn't work for the aiMagic because it would still cast magic if it was <= 9 but attack is <= 7. It works for attack and heal but I need magic to be 8 or 9 only. If it is <= 9 then odds are it will also be <= 7 which is what attack is.
Either I don't understand your question or I need to explain else-statements.
You want to call aiAttack if aiNumber is 1-7, aiMagic if it is 8-9 and aiHeal if it is 10?
Because this is what the code I posted does, if you want I can try to explain else-statements or you can just google it.

No I understand them completely. I tried your code and traced what the number would be every time it aiMagic was called and I got numbers below 9 all the way down to 1. Think about it.... <= 9 .... so the number can be less than 9 but it never stops at 8....

So I fixed it by doing this:

if (aiNumber == 8 || aiNumber == 9)
        {
              aiMagic();
         }

BBS Signature
MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to Check range within math.random? Feb. 2nd, 2014 @ 01:35 AM Reply

At 2/2/14 01:31 AM, Hero101 wrote: No I understand them completely. I tried your code and traced what the number would be every time it aiMagic was called and I got numbers below 9 all the way down to 1. Think about it.... <= 9 .... so the number can be less than 9 but it never stops at 8....

His code says

If the number is less than or equal to 7, attack
otherwise, if it's less than or equal to 9, magic
otherwise, heal

The else that it seems like you didn't type is what restricts the 2nd conditional to the numbers 8 and 9. If it's less than 8, the first conditional takes care of it and the 2nd one never runs.

Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Response to Check range within math.random? Feb. 2nd, 2014 @ 01:41 AM Reply

The else that it seems like you didn't type is what restricts the 2nd conditional to the numbers 8 and 9. If it's less than 8, the first conditional takes care of it and the 2nd one never runs.

Ooooh damn I didn't see the "else" in the "else if" of the second condition.... Forgive me I'm not all there tonight...

Thank you guys...


BBS Signature
Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Check range within math.random? Feb. 2nd, 2014 @ 01:43 AM Reply

At 2/2/14 01:31 AM, Hero101 wrote:

No I understand them completely. I tried your code and traced what the number would be every time it aiMagic was called and I got numbers below 9 all the way down to 1. Think about it.... <= 9 .... so the number can be less than 9 but it never stops at 8....


So I fixed it by doing this:

if (aiNumber == 8 || aiNumber == 9)
{
aiMagic();
}

If you copied my code correctly it is impossible to reach the line which calls aiMagic with other numbers than 8 and 9.

if the number is 1-7:

if (aiNumber <= 7) // true
{
	aiAttack(); //<---executed
}
else if (...) // this will not be executed, because previous if was true

if the number is 8-9:

if (aiNumber <= 7) // false, else statement will be executed
{
		aiAttack();
}
else if (aiNumber <= 9) //true
{
	aiMagic();//<---executed
}
else ... // this will not be executed, because previous if was true

if the number is 10:

if (aiNumber <= 7) // false, else statement will be executed
{
	aiAttack();
}
else if (aiNumber <= 9) // false, else statement will be executed
{
	aiMagic();
}
else
{
	aiHeal();//<---executed
}

this code is more or less the same as:

if (aiNumber <= 7)
{
	aiAttack();
}
if (7 < aiNumber && aiNumber <= 9) // or "if (aiNumber == 8 || aiNumber == 9)" like you did it
{
	aiMagic();
}
if (9 < aiNumber)
{
	aiHeal();
}
Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Check range within math.random? Feb. 2nd, 2014 @ 01:46 AM Reply

At 2/2/14 01:41 AM, Hero101 wrote:
The else that it seems like you didn't type is what restricts the 2nd conditional to the numbers 8 and 9. If it's less than 8, the first conditional takes care of it and the 2nd one never runs.
Ooooh damn I didn't see the "else" in the "else if" of the second condition.... Forgive me I'm not all there tonight...

Thank you guys...

Ok, forget my previous post, I was slow posting it and MSGhero's answer is better anyways.
Glad it works now.

Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Response to Check range within math.random? Feb. 3rd, 2014 @ 01:03 AM Reply

Ok, forget my previous post, I was slow posting it and MSGhero's answer is better anyways.
Glad it works now.

Haha it's all good. Thank you guys for all your help. Thanks to the help in this post I am on the home stretch of finishing my game.


BBS Signature
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Response to Check range within math.random? Feb. 3rd, 2014 @ 06:51 PM Reply

Ok, forget my previous post, I was slow posting it and MSGhero's answer is better anyways.
Glad it works now.

I have one last quick question for you dude. Based on the code you gave me posted above in this post (which works - thank you), I was wonder if in theory you could do this a ton of "else if" statements. Like:

if ( //insert parameter check)
{
    //run code
}
else if ( //insert parameter check)
{
    //run code
}
else if ( //insert parameter check)
{
    //run code
}
else if ( //insert parameter check)
{
    //run code
}
else if ( //insert parameter check)
{
    //run code
}
else if ( //insert parameter check)
{
    //run code
}
else if ( //insert parameter check)
{
    //run code
}
else
{
    //run code
}

Now I'm not sure why anyone would need to do a ton of else/if statements, but I was wondering if in theory if this would work?


BBS Signature
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Check range within math.random? Feb. 3rd, 2014 @ 07:24 PM Reply

At 2/3/14 06:51 PM, Hero101 wrote: but I was wondering if in theory if this would work?

Yes, you can have long chains of if/else statements.

An alternative way is to use a switch statement, which is not too different.
It makes it more clear that you are deciding between more than two different choices.

As you are just calling functions, you could simply stick them into an array.
This will work no matter how many "actions" you have.

// some untested code sketch to illustrate

var actions:Array = [aiAttack, aiMagic, aiHeal];

function doRandomAction():void
{
    actions[Math.ceil(Math.random() * actions.length)]();
}
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Check range within math.random? Feb. 3rd, 2014 @ 08:13 PM Reply

At 2/3/14 07:24 PM, milchreis wrote: actions[Math.ceil(Math.random() * actions.length)]();

You need to floor that, otherwise the range is 1-actions.length.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Check range within math.random? Feb. 3rd, 2014 @ 09:18 PM Reply

At 2/3/14 08:13 PM, Diki wrote:
At 2/3/14 07:24 PM, milchreis wrote: actions[Math.ceil(Math.random() * actions.length)]();
You need to floor that, otherwise the range is 1-actions.length.

right, my bad

Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Check range within math.random? Feb. 3rd, 2014 @ 09:56 PM Reply

At 2/3/14 06:51 PM, Hero101 wrote:
Now I'm not sure why anyone would need to do a ton of else/if statements, but I was wondering if in theory if this would work?

As stated before, it would work, but I would avoid it if possible.

You can do it like milchreis said and just put aiAttack 7 times into the array and aiMagic 2 times to get the probabilities from your example, or you could use the analog switch case:

switch(aiNumber)
{
	case 8:
	case 9:
		aiMagic();
		break;
	case 10:
		aiHeal();
		break;
	default:
		aiAttack();
		break;
}

I personally would use a loop if I had to scale up your example, which would look something like this:
(with less hardcoding and as an own function)

var functions:Array = [aiAttack, aiMagic, aiHeal];
var limits:Array = [7, 9, 10];

var randomNum:Number = Math.ceil(Math.random() * limits[limits.length - 1]);

for (var i:int = 0; limits[i] < randomNum; i++) 
{
}

functions[i]();

I prefer the loop because it could handle floating point limits with little change and i like probabilities to sum up to 1 and it's easier to make a function out of it (limits as input, i as output).

Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Response to Check range within math.random? Feb. 3rd, 2014 @ 10:10 PM Reply

At 2/3/14 07:24 PM, milchreis wrote:
At 2/3/14 06:51 PM, Hero101 wrote: but I was wondering if in theory if this would work?
Yes, you can have long chains of if/else statements.

An alternative way is to use a switch statement, which is not too different.
It makes it more clear that you are deciding between more than two different choices.

As you are just calling functions, you could simply stick them into an array.
This will work no matter how many "actions" you have.

// some untested code sketch to illustrate

var actions:Array = [aiAttack, aiMagic, aiHeal];

function doRandomAction():void
{
actions[Math.ceil(Math.random() * actions.length)]();
}

Oh that's really interesting! To be honest I don't know anything about Arrays yet :( I've been learning through a simple book so far and it touches on them in the next chapter that I haven't got around to reading. Guess I will also have to learn through online tutorials too because I constantly hear about different ways to use Arrays in games here on the forums. When I look at the code for some of the arrays it confuses me. I know the first thing I would like to do with Arrays is learn to put like a collectible (like coins from Mario) in it so that way I can reference use them in a game without have to have an instance name for each and every coin on the stage/level. I was told I would need to do this with Arrays but no one showed me a coding example. I guess that's for the best as it will make me learn on my own and potentially make me understand them better. I will definitely be diving into learning Arrays after I finish this first game.


BBS Signature
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Response to Check range within math.random? Feb. 3rd, 2014 @ 10:15 PM Reply

You can do it like milchreis said and just put aiAttack 7 times into the array and aiMagic 2 times to get the probabilities from your example, or you could use the analog switch case:

Yeah as I just posted I will definitely have to spend a lot of time learning Arrays as my knowledge of them is zero at the moment :( Once I finish this simple game I'm gonna dive into learning them.

Also I've never heard of a switch case... I will need to look into learning them as well.

Thank you for your constant input. I really appreciate it :)


BBS Signature
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Check range within math.random? Feb. 4th, 2014 @ 05:25 AM Reply

At 2/3/14 09:56 PM, Etherblood wrote: You can do it like milchreis said and just put aiAttack 7 times into the array and aiMagic 2 times to get the probabilities from your example,

The example was for functions of the same probability. I would not add them multiple times for different probabilities.

I personally would use a loop if I had to scale up your example, which would look something like this:
(with less hardcoding and as an own function)

var functions:Array = [aiAttack, aiMagic, aiHeal];
var limits:Array = [7, 9, 10];

I'd use this approach, too, but with a single array array.
Keeping two arrays in sync is not a good idea.

var actions:Array = [{limit: 7, action: aiAttack}, 
                                      {limit: 9, action:aiMagic},
                                      {limit: 10, action: aiHeal}];
kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to Check range within math.random? Feb. 4th, 2014 @ 06:47 AM Reply

At 2/4/14 05:25 AM, milchreis wrote: Keeping two arrays in sync is not a good idea.

Why is it not a good idea?
Because it's harder to maintain than objects?
But what takes less memory and works faster - objects or synchronized arrays?


BBS Signature
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Check range within math.random? Feb. 4th, 2014 @ 07:17 AM Reply

At 2/4/14 06:47 AM, kkots wrote:
At 2/4/14 05:25 AM, milchreis wrote: Keeping two arrays in sync is not a good idea.
Why is it not a good idea?
Because it's harder to maintain than objects?

yes

a probability is always tied to a certain function

But what takes less memory and works faster - objects or synchronized arrays?

idk, it doesn't really matter in this case and I doubt it ever will

Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Check range within math.random? Feb. 4th, 2014 @ 07:53 AM Reply

At 2/4/14 07:17 AM, milchreis wrote: a probability is always tied to a certain function

But what takes less memory and works faster - objects or synchronized arrays?
idk, it doesn't really matter in this case and I doubt it ever will

I agree that it's easier to mess up with synced arrays, but think that both approaches are valid.
I imagined my helper function to look like this:

public static function randomIndex(probabilities:Array):int
{
	var limits:Array = [];
	var sum:Number = 0;
	for (var i:int = 0; i < probabilities.length; i++)
	{
		sum += probabilities[i];
		limits.push(sum);
	}
	var randomNum:Number = Math.random() * sum;
	
	for (i = 0; limits[i] < randomNum; i++)
	{
	}
	
	return i;
}

which is much easier to use with seperate arrays.
It's usage in our example could look like this:

[aiAttack, aiMagic, aiHeal][randomIndex([0.7, 0.2, 0.1])](); //short and dirty...
kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to Check range within math.random? Feb. 4th, 2014 @ 08:35 AM Reply

At 2/4/14 07:53 AM, Etherblood wrote: [aiAttack, aiMagic, aiHeal][randomIndex([0.7, 0.2, 0.1])](); //short and dirty...

Short and dirty is exactly why we don't like this approach. We want a full-blown table of functions with their pseudo-probability numbers (pseudo, because a probability is from 0 to 1, and those values more like define a range or smth). That table would be much more comfortable to maintain of you have 100 functions (assume that).

var table:Array=[
	{range:0.2,func:aiHeal},
	{range:0.1,func:aiAttack},
	{range:1,func:aiDefend}
];
var randomFunction:Function=getRandom(table);
function getRandom(table:Array):Function{
	var totalRange:Number=0;
	for(var i:uint=0;i<table.length;i++){
		totalRange+=table[i].range;
	}
	var randomNumber:Number=Math.random()*totalRange;
	var sum:Number=0;
	for(i=0;i<table.length;i++){
		if(randomNumber>sum+table[i].range)return table[i].func;else
			sum+=table[i].range;
	}
}

code untested, written right here


BBS Signature
kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to Check range within math.random? Feb. 4th, 2014 @ 08:45 AM Reply

At 2/4/14 08:35 AM, kkots wrote:

Sorry, mistake in this code.

for(i=0;i<table.length;i++){
if(randomNumber>sum+table[i].range)return table[i].func;else
sum+=table[i].range;
}
}

It should be

for(i=table.length-1;i>=0;i--){
		if(randomNumber>totalRange-sum-table[i].range)return table[i].func;else
			sum+=table[i].range;
	}
}
At 2/4/14 07:53 AM, Etherblood wrote: for (i = 0; limits[i] < randomNum; i++)
{
}

return i;
}

Your code has errors too
It should be

for(i=limits.length-1; limits[i]<randomNum; i--){}
return i;

lolol


BBS Signature
Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Check range within math.random? Feb. 4th, 2014 @ 09:27 AM Reply

At 2/4/14 08:45 AM, kkots wrote: Sorry, mistake in this code.
for(i=0;i<table.length;i++){
if(randomNumber>sum+table[i].range)return table[i].func;else
sum+=table[i].range;
}
}

Replacing > with < should fix it I think

It should be

for(i=table.length-1;i>=0;i--){
if(randomNumber>totalRange-sum-table[i].range)return table[i].func;else
sum+=table[i].range;
}
}

This is wrong, try with randomNumber = 0, the loop will finish without returning assuming range is always positive


At 2/4/14 07:53 AM, Etherblood wrote: for (i = 0; limits[i] < randomNum; i++)
{
}

return i;
}
Your code has errors too

Yup, it should be <= instead of <

It should be

for(i=limits.length-1; limits[i]<randomNum; i--){}
return i;

lolol

Nope, this is wrong again, try the randomNum = 0 example it will return the last index when 0 should be returned

You forgot the return at the end for the compiler.
Didn't notice that the limit array creation in my code was unnecessary, thx.
I like to make my helper functions as general as possible to be able to recycle it, so I'd change the return type and the value types in the array to Object. This way it'd be easier to use this function for other stuff.

Changed my code:

public static function randomIndex(probabilities:Array):int
{
	var sum:Number = 0;
	for (var i:int = 0; i < probabilities.length; i++)
	{
		sum += probabilities[i];
	}
	var randomNum:Number = Math.random() * sum;
	
	sum = 0;
	for (i = 0; (sum += probabilities[i]) <= randomNum; i++){}
	
	return i;
}

And added a table version like yours:
(table = {range:x, element:y})

public static function randomElement(table:Array):int
{
	var sum:Number = 0;
	for (var i:int = 0; i < table.length; i++)
	{
		sum += table[i].range;
	}
	var randomNum:Number = Math.random() * sum;
	
	sum = 0;
	for (i = 0; (sum += table[i].range) <= randomNum; i++){}
	
	return table[i].element;
}
kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to Check range within math.random? Feb. 4th, 2014 @ 09:50 AM Reply

At 2/4/14 09:27 AM, Etherblood wrote: This is wrong, try with randomNumber = 0, the loop will finish without returning assuming range is always positive

randomNumber will never be equal to 0, nor to sum. You can assume that it has a very low value, but not 0. Therefore my code is correct.

Yup, it should be <= instead of <

Why <= ? randomNumber is a floating point number, it will never be exactly equal to anything.

Changed my code:

The code is correct.

And added a table version like yours:

Correct code.

Nice. Hope the original poster will find this useful. If he is not dead from our programmer talk already.


BBS Signature
Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Check range within math.random? Feb. 4th, 2014 @ 10:11 AM Reply

At 2/4/14 09:50 AM, kkots wrote: randomNumber will never be equal to 0, nor to sum. You can assume that it has a very low value, but not 0. Therefore my code is correct.

Math.random() "Returns a pseudo-random number n, where 0 <= n < 1."
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Math.html#random()
randomNumber can become equal to 0, but not to sum

Why <= ? randomNumber is a floating point number, it will never be exactly equal to anything.

Floating point numbers can be equal, it's just rare in most cases.
And for why < was wrong:
randomIndex([0, 1]) could return 0 (if randomNum was 0), but the result should always be 1

Nice. Hope the original poster will find this useful. If he is not dead from our programmer talk already.

Maybe if I added comments to my code...

Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Check range within math.random? Feb. 4th, 2014 @ 10:29 AM Reply

my randomElement's return value should be Object, not int

public static function randomElement(table:Array):<strong>Object</strong>
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 21
Game Developer
Response to Check range within math.random? Feb. 4th, 2014 @ 08:06 PM Reply

At 2/4/14 09:50 AM, kkots wrote: Nice. Hope the original poster will find this useful. If he is not dead from our programmer talk already.

Hahahaha! Nope not dead. Just a bit overwhelmed. I really need to learn about Arrays and Loops. Then I could follow a little better. I really do appreciate all the feedback guys. I will be referring back to this thread after I've taught myself how Arrays and Loops work.

Thanks again everybody :)


BBS Signature
kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to Check range within math.random? Feb. 4th, 2014 @ 08:34 PM Reply

At 2/4/14 10:29 AM, Etherblood wrote: my randomElement's return value should be Object, not int

public static function randomElement(table:Array):Object

Naa, it doesn't matter. That's minor stuff. Everybody should just modify the function to their liking. For ex. table[i].whatever instead of table[i].element or just return table[i]. Same with static namespace (what if the function is on a frame). You wrote the base correctly, and that's enough and the best you can achieve.

At 2/4/14 08:06 PM, Hero101 wrote:
At 2/4/14 09:50 AM, kkots wrote: Nice. Hope the original poster will find this useful. If he is not dead from our programmer talk already.
Hahahaha! Nope not dead. Just a bit overwhelmed. I really need to learn about Arrays and Loops. Then I could follow a little better.

Yeah have fun, it took me quite a bit of time to follow Etherblood's code, but that was really exciting. As if I'm solving some super difficult puzzle, you know, or more like untying shoelaces tied by another person in a weird way.

Thanks again everybody :)

NP, see you when you're able to code arrays. Should take you a couple of days to learn.


BBS Signature