Question

I want to know if there is a way to use percentages to make text occur less or more frequently, like if I want word1 to occur 1% of the time, word2 40% of the time and word3 to occur 59% of the time.

Was it helpful?

Solution 2

The simplest way is to run RNG (Random) (based on time or something dynamic) lets say from 1 - 100 then you can check if the number you got is less or equal to 1 put the word that you want to be with 1% chance etc... if you got another number bigger than 1 then put the word that has 99% of occurrence -> it is simplified.

however it might be not the best solution because random is not really random (but it is another problem ).

back to the original question word1 -> 1% is <=1 word2 -> 40% is >1 && <= 40 word3 -> could be else

OTHER TIPS

If you want simple percentages, randomly pick a number between [0,99], and "assign" ranges to different outcomes. Simply,

- (NSString*)randomWord
{
    uint32_t r = arc4random_uniform(100);
    if (r < 1) return word1;
    if (r < 41) return word2;
    return word3;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top