X Percent of a Chance [solved]

Started by Snake, Mon 18/05/2009 19:02:16

Previous topic - Next topic

Snake

Hello all you AGS scripting savvy sons a...

I've looked through the manual but haven't been able to find this.

Am I able to use percents while randomizing? What I'm aiming for here is to give an object a 60/40 percent chance of executing a specific action once it's script is executed. Is this possible?

Please go slow and easy with me, I've never understood or been good with math.

So if not, what I would normally do in this situation would be to set 10 random actions and make six of them do one thing and 4 do another.

I don't mind doing it that way, but I'd prefer a faster, more efficient way.


And thanks in advance!
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

DoorKnobHandle

A simple way would be to do this:

Code: ags

// get a random value ranging from 0-99
int rand = Random ( 99 );

if ( rand < 40 )
// the following block of code has a 40 percent chance of getting called
{
      // do the 40 percent thing
}
else
// the rest has a 60 percent change of getting called
{
      // do the 60 percent thing
}


If you only work with 0, 10, 20, 30, 40 etc. percent you can divide all that by 10 of course.

Does that make sense or do you need a more in-detail description?

The above way is a bit simplified btw, it's actually a 39 to 61 percent chance but I guess it doesn't matter with these numbers and it's easier to read like this.

Trent R

I believe this should work with what you want. (though actually, you could simplify it down to Random(5) )

Code: ags
int ran=Random(10);
if (ran<6) {  //0,1,2,3,4,5 = 6 possibilities
  //60 percent chance to activate this branch
}
else {
  //40 percent chance
}


[Edit]: Beat by dkh.
~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

Snake

Ah-ha, got it, that should work quite well. Don't know why I didn't think of that.

Dkh, why did you choose 99?  Couldn't you have put 100 instead? Oh wait, 0 is counted, isn't it.

Thanks guys for the quick responce. I'll come back if I've got any problems with it.
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

DoorKnobHandle

I picked 99 because giving something a probability of zero percent wouldn't really make sense... :) So, the int rand would be anything from 0-99 but I'd actually think of the number as one more leaving me with probabilities between one percent and 100.

GarageGothic

#5
If you're going to use this multiple times throughout the game (and only to check for success/failure), the easiest thing would be to write a function like this:

Code: ags
bool SuccessChance(int percentchance) {
  if (Random(99) < percentchance) return true;
  else return false; //I don't think this is really necessary though, since "false" is the default for bools
  }


This also allows probability of 0, which will always return false, and 100 which always returns true.

monkey0506

#6
That could even be simplified down GG:

Code: ags
bool SuccessChance(int percentchance) {
  return (Random(99) < percentchance);
}


Since the < is a boolean operator you can use it directly this way. And although returning false explicitly may not be necessary it is better programming practice. Making it a habit to always explicitly state the return value ensures that if anything ever changes in the way AGS handles things (unspecified return values) you always know exactly what's actually getting returned.

Oh and also, since the if-statement will return from the function if true, it's not necessary to use an else clause. But that's another story. 8)

By the way Snake, using the seed value of 99 also allows more precision than Trent's example does, for example you can make the percentage a 66% as opposed to only 60% or 70%. Further, a higher random seed means a greater range of possibility, producing a more random result. ;)

GarageGothic

Yeah, that's true monkey. I just avoid that style of coding because I find it hard to troubleshoot. Obviously this isn't a problem in a script of this length, but I generally prefer clear "if... then" statements. I only recently started using "if (!value)" statements and am still not sure it's a good idea for me.

Snake

Right, I agree, GG. I am going to be using it throughout, so a function is certainly the best branch to choose.

QuoteAnd although returning false explicitly may not be necessary it is better programming practice.
I do a lot of things that are unnecessary. I get confused very easily and so program so I know exactly what is going on. Lately I've also been making sure I comment everything. A week from now I won't know what the hell int frntrw2=0; is.
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

SMF spam blocked by CleanTalk