Hi there! You are currently browsing as a guest. Why not create an account? Then you get less ads, can thank creators, post feedback, keep a list of your favourites, and more!
Quick Reply
Search this Thread
Test Subject
Original Poster
#1 Old 17th May 2022 at 5:01 AM
Default The mystery of kChanceOf...
Hi everyone!

I am here again to ask you about something in the scripting universe that I can't figure out myself by reading others codes or doing some research here and there...

For me, the kChanceOf... is a complete mystery of how it work and how to implement it in my work...

There's two examples of how I would like to learn how to use it and how it is working:


1: In the case of a skill interaction like painting, for example, how can we use/add the kChanceOf... so the resulting painting will be a Masterpiece, Brilliant painting, etc. ?
(I would like to add this option to one of my custom skill mod to make it more interesting. For now I'm only using the RandomUtil to make the value random depending on skill level just like in the code below )


2: In the same sort of idea as the first point, I would like to add, for example, a 25% chance that my script would refuse to run the script (in this example to ModifyFunds) when I use an interaction (like in this code) and adding a negative message to the interaction to let me know that the 25% chance happened:

Code:
if (this.Actor.HasInventory)
            {
                base.StartStages();
                bool flag = base.DoLoop(ExitReason.Default);
                if (this.Actor.Inventory.ContainsType(typeof(Object1), 1))
                {
                    Object1 obj1 = this.Actor.Inventory.Find<Object1>();
                    if (obj1 == null)
                    {
                        return false;
                    }
                    base.BeginCommodityUpdates();
                    int @int = RandomUtil.GetInt(Class.kTestMin[skilllevel], Class.kTestMax[skilllevel]);
                    this.Actor.ModifyFunds(@int);
                    obj1.Destroy();
                }
                if (this.Actor.Inventory.ContainsType(typeof(Object2), 1))
                {
                    Object2 obj2 = this.Actor.Inventory.Find<Object2>();
                    if (obj2 == null)
                    {
                        return false;
                    }
                    base.BeginCommodityUpdates();
                    int @int = RandomUtil.GetInt(Class.kTestMin[skilllevel], Class.kTestMax[skilllevel]);
                    this.Actor.ModifyFunds(@int);
                    obj2.Destroy();
                }
                base.EndCommodityUpdates(flag);
                return flag;


Thanks again for your help and your time!

P16
Advertisement
Space Pony
#2 Old 17th May 2022 at 6:03 PM
If you wanted a 25% chance of running some code, you would do something like:

Code:
if (RandomUtil.RandomChance(25f))
{
    // 25% chance this code runs
}
else
{
    // 75% chance this code runs
}


RandomChance takes a float value between 0 and 100, where 0 is guaranteed never to happen and 100 will always happen. Tunable values like "kChanceOfX" can be passed to the function if you want user-adjustable probabilities. For example, painting quality is determined using the method Sims3.Gameplay.Skills.PaintingSkill.PickPaintingQualityAndLevel:

Code:
if (lvl >= PaintingSkill.kLevelCanMakeMasterpiece)
{
    int num = PaintingSkill.kChanceMasterpiece;
    if (skill2 != null && skill2.IsProficientPainter)
    {
        num = PaintingSkill.kChanceMasterpieceForProficientPainter;
    }
    else if (s.HasTrait(TraitNames.ExtraCreative))
    {
        num += TraitTuning.ExtraCreativeMasterpieceChanceAdd;
    }
    else if (s.HasTrait(TraitNames.Perfectionist))
    {
        num += TraitTuning.PerfectionistTraitPaintingMasterpieceChanceAdd;
    }
    if (RandomUtil.RandomChance((float)num))
    {
        q = PaintingSkill.PaintingQuality.Masterpiece;
    }
}
if (lvl >= PaintingSkill.kLevelCanMakeBrilliant && q != PaintingSkill.PaintingQuality.Masterpiece)
{
    int num2 = PaintingSkill.kChanceBrilliant;
    if (skill2 != null && skill2.IsProficientPainter)
    {
        num2 = PaintingSkill.kChanceBrilliantForProficientPainter;
    }
    else if (s.HasTrait(TraitNames.ExtraCreative))
    {
        num2 += TraitTuning.ExtraCreativeBrilliantChanceAdd;
    }
    else if (s.HasTrait(TraitNames.Perfectionist))
    {
        num2 += TraitTuning.PerfectionistTraitPaintingBrilliantChanceAdd;
    }
    if (RandomUtil.RandomChance((float)num2))
    {
        q = PaintingSkill.PaintingQuality.Brilliant;
    }
}


Basically, if the Sim's painting skill level is above the threshold for painting masterpieces, there will be a base chance kChanceMasterpiece that the painting will be a masterpiece. If it's not a masterpiece and the skill level allows for brilliant paintings, there will be a base chance kChanceBrilliant that the painting will be brilliant. These base chances are then potentially adjusted by other trait- and skill-related conditions, again controlled by tuning.

"The Internet is the first thing that humanity has built that humanity doesn't understand, the largest experiment in anarchy that we have ever had." - Eric Schmidt

If you enjoy the mods I put out, consider supporting me on patreon: www.patreon.com/Gamefreak130
Test Subject
Original Poster
#3 Old 18th May 2022 at 4:36 AM
Wow, thanks a lot gamefreak130, now it makes sense!

I was wondering how a simple number could make a code to run at a certain degree of chance.

I don't know, in my head, I thought, if I call the RandomUtil and then put a float with it, this will give me a random number for XP, $, or anything else, but could not figure out how to turn that number into probabilities...


Well, I hope that this will help not only myself, but everyone who was struggling like me to figure out the mystery of the kChanceOf...


P16
Test Subject
Original Poster
#4 Old 3rd Jun 2022 at 4:41 AM
I had a question in my head since you've answered this thread and I've decided that today I need the answer! (and see if I begin to understand well the coding universe!)

In the case that I want three, four or more percentage chance of running some code, correct me if I am wrong, but do I need to add an else if and then re-enter a percentage value to each of them?

For example:
Code:
if (RandomUtil.RandomChance(50f))
{
}
else if (RandomUtil.RandomChance(45f))
{
}
else if (RandomUtil.RandomChance(4f))
{
}
else (RandomUtil.RandomChance(1f))
{
}
Space Pony
#5 Old 4th Jun 2022 at 12:08 AM
Because you're running independent RandomChance events before each if statement, the code you listed will actually have a 50% chance of running the first block, a (50% * 45% = 22.5%) chance of the second block, etc. Think of it like flipping a coin, then flipping the coin again only if you got tails the first time. You'd have a 50% chance of getting heads on your first flip, a 50% chance to get a second flip, but only a 25% chance that you get a second flip and that second flip is heads (50% * 50% = 25%).

Instead, I think what you want to do is something like:

Code:
float num = RandomUtil.GetFloat(100);
if (num < 50)
{
}
else if (num < 45)
{
}
else if (num < 4)
{
}
else if (num < 1)
{
}


This will randomly generate a single number between 0 and 100, meaning that there is a 50% chance it will be less than 50, a 45% chance it is less than 45, etc.

"The Internet is the first thing that humanity has built that humanity doesn't understand, the largest experiment in anarchy that we have ever had." - Eric Schmidt

If you enjoy the mods I put out, consider supporting me on patreon: www.patreon.com/Gamefreak130
Space Pony
#6 Old 4th Jun 2022 at 12:36 AM
Quote: Originally posted by gamefreak130
Because you're running independent RandomChance events before each if statement, the code you listed will actually have a 50% chance of running the first block, a (50% * 45% = 22.5%) chance of the second block, etc. Think of it like flipping a coin, then flipping the coin again only if you got tails the first time. You'd have a 50% chance of getting heads on your first flip, a 50% chance to get a second flip, but only a 25% chance that you get a second flip and that second flip is heads (50% * 50% = 25%).

Instead, I think what you want to do is something like:

Code:
float num = RandomUtil.GetFloat(100);
if (num < 50)
{
}
else if (num < 45)
{
}
else if (num < 4)
{
}
else if (num < 1)
{
}


This will randomly generate a single number between 0 and 100, meaning that there is a 50% chance it will be less than 50, a 45% chance it is less than 45, etc.


You have to reverse it though to make it work

Code:
float num = RandomUtil.GetFloat(100);
if (num < 1)
{
}
else if (num < 4)
{
}
else if (num < 45)
{
}
else if (num < 50)
{
}
Space Pony
#7 Old 4th Jun 2022 at 1:00 AM
Yep, thanks for catching that.

"The Internet is the first thing that humanity has built that humanity doesn't understand, the largest experiment in anarchy that we have ever had." - Eric Schmidt

If you enjoy the mods I put out, consider supporting me on patreon: www.patreon.com/Gamefreak130
Test Subject
Original Poster
#8 Old 4th Jun 2022 at 3:48 AM
Thank you to both of you it makes a lot of sense! I never thought of making my own percentage based on a number like that!

At least I was partly right with the else if...
Back to top