Using "random" properly with ifelse command

Started by thepacifist, May 23, 2009, 06:26:17 AM

Previous topic - Next topic

thepacifist

I'm trying to configure a value for an AI move using ifelse and random. Say I have four or five different values I'd like to randomize at the same likelihood for each. How would I do that? My guess was this (for 4 possible values):
value = ifelse((random=[1,250]),200,(ifelse((random=[251,500]),230,(ifelse((random=[501,750]),400,430)))))
This is how I would split the probability among four values.

But then I realized that I'm not sure if at each "random=x,y" juncture the random is out of 1000 again or what's left from the previous junctures (sorry about the wording, not sure what the proper jargon is here). If at each random juncture the random number is out of 1000 again, then I would need to fix the above code. But if it was only out of what was left from the previous junctures, then the above code should work. As I've been testing it, it seems more likely that at each juncture the random is out of another pure 1000, not what's left over from previous randoms. But just wanted to check and see if this was right.

If each random juncture were out of 1000 and not what was left, then, if I want to make it equally likely that any of four values was chosen, should I code it as follows:
value = ifelse((random<500),200,(ifelse((random<500]),230,(ifelse((random<500]),400,430)))))

Thanks for any help clarifying this.


Cyanide

Every time random is evaluated it's evaluated uniquely.


In M.U.G.E.N there is no magic button

They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance.

MikeDaBike

Quote from: Cyanide on May 23, 2009, 07:55:06 AM
Every time random is evaluated it's evaluated uniquely.

And that's the reason why your first example would actually work, the probabililty for Random to be between 1 and 250 and between 251 and 500 is the same. However each value has a different probability in the end because the last values are dependent on the former random values (I hope this is understandable, it's hard to explain)

For this specific values I would use something like this:
value = 200+200*(Random<500)+30*(Random<500)
Latest Creation: SSBB Assist Trophies

Bastard Mami

ifelse((random=[1,250]),200,(ifelse((random=[0,332]),230,(ifelse((random=[500,999]),400,430)))))

one out of 4 you get the first choice, if not there are 3 choices left, one out of 3 you get the seond choice, if not, there are 2 choices left, one out of 2 you get the first choice. apparently raqndom is not spread properly, so using the % operator gives statistically better results.

ifelse((random%4=0),200,(ifelse((random%3=0),230,(ifelse((random%2=0),400,430)))))

fatnlazy


thepacifist

:o Wow, you're all awesome! This was all so clarifying and enlightening. Thanks for answering my question and tweaking my code. I also had no idea about the % operator! I knew this was the right place to ask this question. :)

Hopefully my AI coding will be less predictable and more reliable now.  ;)


Messatsu

Quote from: MikeDaBike on May 23, 2009, 12:55:05 PM
And that's the reason why your first example would actually work, the probabililty for Random to be between 1 and 250 and between 251 and 500 is the same.
Under the assumption that Mugen implements random in the documented way.

Quote from: Bastard Wolf on May 12, 2008, 05:02:05 PM
Many people risk their lives everyday by having Mugen.

Ricepigeon

Theoretically speaking, if you wanted each random to not be calculated uniquely, you could just designate a variable to hold a random number and use that variable instead, no? Or you could apply some algorithm to the random in conjunction with the random call (essentially creating a PRNG, albeit not as "random" as MUGEN's default.)

Messatsu

I think that is better than continually calling random inside of ifelse statements.  When most people want random, they want different, not statistically random, so either is probably fine.  No real theory to it, you can do it.

Quote from: Bastard Wolf on May 12, 2008, 05:02:05 PM
Many people risk their lives everyday by having Mugen.

MikeDaBike

Quote from: Messatsu on May 24, 2009, 07:23:48 AM
Quote from: MikeDaBike on May 23, 2009, 12:55:05 PM
And that's the reason why your first example would actually work, the probabililty for Random to be between 1 and 250 and between 251 and 500 is the same.
Under the assumption that Mugen implements random in the documented way.

I dont know the history about it, but has this ever been proven not to work like documented?
In AI programming most people use triggers like Random<100, if Random doesnt give equally probable values this would have different results than for example Random>=900.
Latest Creation: SSBB Assist Trophies

Messatsu

I'm saying it might not be statistically random (over 1 million trials you should get a pretty even distribution).  I'm too lazy to check though.

Quote from: Bastard Wolf on May 12, 2008, 05:02:05 PM
Many people risk their lives everyday by having Mugen.

Cyanide

random is more random than varrandom.

I've found, and some others have noticed as well that varrandom tends towards the lower numbers.

varrandom of 0-5 will give you 0,1,2 FAR more often than 3,4,5.

Due to random being evaluated every tick. Unless you absolutely NEED something to happen in a given situation your values should be really low.

In an AI i recommend sub 100 for most specials and any comboing. 5's and 10's are well enough for most of an AI. Doing more just gives you spam.


In M.U.G.E.N there is no magic button

They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance.

thepacifist

Impressive. This seems like the edge of MUGEN AI theory. I'll need to start trying to use some of these other ways of spicing up a char's AI moves. Right now I only use what I know, but apparently I only know the tip of the iceberg when it comes to randomizing or diversifying a char's AI moves. Thanks for stretching me here.

Also, thanks for the stylistic feedback on the range of random to use (i.e. sub 100). My AI's definitely spam a lot at the moment. As I began creating them, that was how I thought they could be challenging. But I suppose all the spamming can turn a character into Mecha-Godzilla from his first encounter with Godzilla (old series). I guess its time for me to experiment.

Byakko

QuoteIn an AI i recommend sub 100 for most specials and any comboing. 5's and 10's are well enough for most of an AI. Doing more just gives you spam.
Depends if you add other detailed triggers such as the position of the enemy. Which you will get to do anyway if you want to make an over-the-top AI.
If I struggled to the end of my determination, to the end of my way of life with my followers, if the result is ruin, then this ruin is inevitable. Grieve. Shed tears. But you cannot regret.

Bastard Mami

I use var (X) = random, that way I keep the probability checking simple.