# How can I randomly generate a unit instance based on a probability?

I want AirUnit to return a value between 1 and 3. I want LandUnit to return a value between 4 and 6. I want WaterUnit to return a value between 7 and 9.

The Array is suposed to be inside this function:

public function getRandom():Unit
{
// Array
}
-
Your function isn't returning a number, it's returning a Unit. It sounds like you're inverting the problem. What's your end goal? – michael.bartnett Nov 9 '11 at 14:42
I want to put WaterUnit, LandUnit and AirUnit in a Array and if the generated number of the array is 1-3 its a WaterUnit and if the generated number is 4-6 its a LandUnit and if the generated number is 7-9 its a AirUnit. I want to have a return in my function ... – V.V.T Nov 9 '11 at 14:56
By doing that, you would be reusing those units, not creating new ones. Is that the behavior you want? – Ricket Nov 9 '11 at 15:48

Your chances in your example are always even and therefore do not add any more randomness to the whole situation despite increasing their range, each choice always has the same number of chances to occur. For execution of speed you should just generate a random number of 0, 1 or 2 and use that as an index into the array to return the unit you want. The way most people generate random numbers between a 0 based range is to use a normal rand() call that returns a huge number and then use the modulus operator on it (the % symbol in most languages but I will admit I have not worked in action script in a very long time). So rand() % 3 would give you a number between 0 and 2.

If however what you are asking is for a way to change those values so you can say you will get a land unit 50% of the time, an air unit 30% of the time and a water unit 20% of the time, then the if statements mentioned by others could be changed to accommodate that sort of functionality. Just your original scenario does not show this.

-
Ok, I understand, thanks ... – V.V.T Nov 9 '11 at 17:27
@V.V.T Glad I could help. – James Nov 10 '11 at 3:34

Choose a random number, and then write an if statement to return the appropriate new object based on the number.

I'm not an ActionScript coder so I don't know if this is correct, but generally it would look something like this:

// have an array called 'units' of three elements: an AirUnit, a LandUnit, a WaterUnit
random_num = Math.floor(Math.random()*9)+1; // random number from 1 to 9 inclusive

if(random_num <= 3) {
return units[0]; // the AirUnit
} else if(random_num <= 6) {
return units[1]; // the LandUnit
} else {
return units[2]; // the WaterUnit
}

Again, that might not be exactly correct ActionScript, but that's the general code for what you seem to be asking for...

-
Aaaaaa nice but can you help me showing how the cod for the if statment should be? For ex. I want AirUnit to have a value between 1 and 3. I want LandUnit to have a value between 4 and 6. I want WaterUnit to have a value between 7 and 9. – V.V.T Nov 9 '11 at 14:32
@V.V.T Look up how the random number generator works in the link Ricket provided. If you can't figure out what to do with that, you probably aren't ready to make games - invest more time in learning how to program. – doppelgreener Nov 9 '11 at 15:49
@V.V.T Maybe my edit will help you. This is really basic programming so perhaps you should pick up a beginners' ActionScript book... – Ricket Nov 9 '11 at 15:54
Jonathan Hobbs & Ricket / Its working now, thanks ... Now Im ready to make games hehehehe – V.V.T Nov 9 '11 at 17:23

I want to put WaterUnit, LandUnit and AirUnit in a Array and if the generated number of the array is 1-3 its a WaterUnit and if the generated number is 4-6 its a LandUnit and if the generated number is 7-9 its a AirUnit.

Your end result really has nothing to do with an array -- you just want to generate a random number between 1 and 9 (inclusive) and then, if that number is <= 3, return one type of object, otherwise if it is <= 6, return another type, et cetera.

In pseudo-code, that might look like this:

public function getRandom() : Unit {
value = Math.RandomNumberBetween(1,9);
if(value <= 3 ) {
return new WaterUnit();
} else if(value <= 6) {
return new LandUnit();
} else {
return new AirUnit();
}
}
-
Well maybe, or maybe he's picking one of three different existing objects (or even nine), in which case he would indeed have an array of those objects and would want to return one of them at random. – Ricket Nov 9 '11 at 15:55
That's true -- unless the array is just a local to the function. The question is a bit underspecified so I guessed. – Josh Petrie Nov 9 '11 at 16:06