0

Let's say I have a code for a windows form, that generates greetings when you press a button.

   string[] Greetings = new string[] { "Hi", "Hello", "Howdy!", "Hey" };
   string[] Smilies = new string[] {";)", ":)", "=)", ":-)" };

   Random rand = new Random();
   string Greet = Greetings[rand.Next(0, Greetings.Length)];
   string Smile = Smilies[rand.Next(0, Smilies.Length)];
   TextBox.Text = Greet + " " + Smile;
   Clipboard.SetText(TextBox.Text);

What if I want to add smilies with a probability of X%. So that they do not appear all the time, but with a chance I set in the code? What is a good way to do it?

I thought of something like this --

    public void chance (string source, int probability)
    {
        Random chanceStorage = new Random();
        if (probability >= chanceStorage.Next(0, 100))
            TextBox.Text = source;
    }

And then

    TextBox.Text = Greet;
    chance("_" + Smile, X);

Is that optimal?

Shar1er80
  • 9,001
  • 2
  • 20
  • 29
Uk rain troll
  • 1
  • 1
  • 13
  • 2
    `const int percentageValue = 25; double percentage = (double)percentageValue / 100; if (rand.NextDouble() <= percentage) { \\ do stuff }` – Corak May 27 '15 at 13:30
  • Add as much String.Empty values to your Smilies array until you reach the probability of no smilie you like. – Ralf May 27 '15 at 13:32
  • 3
    @Corak has a good answer (if it were an answer). – ryanyuyu May 27 '15 at 13:33
  • Why not just divide your Smilies length by your probability and generate a random number using that quotient? If the number generated is within the bounds of Smilies' length use the smiley, otherwise you don't use a smiley. So if your probablity is 25%, 4 / 0.25 = 16, you'll generate a random number from 0 - 15 giving you a 25% chance of using a smiley. – Shar1er80 May 27 '15 at 13:36
  • @ryanyuyu - completely untested, so I'm not "sure enough" to put it as an answer. – Corak May 27 '15 at 13:36
  • possible duplicate of [How to create a probability by a given percentage?](http://stackoverflow.com/questions/13342004/how-to-create-a-probability-by-a-given-percentage) – P.K. May 27 '15 at 13:44
  • @P.K. not quite, since the answers seems to address a misconception about probability instead of actually generating a random result with a given probability. – ryanyuyu May 27 '15 at 13:50

3 Answers3

2

50% chance to smile:

   string[] Greetings = new string[] { "Hi", "Hello", "Howdy!", "Hey" };
   string[] Smilies = new string[] {";)", ":)", "=)", ":-)" };

   Random rand = new Random();
   string Greet = Greetings[rand.Next(0, Greetings.Length)];
   string Smile = rand.NextDouble() > 0.5 ? " "+Smilies[rand.Next(0, Smilies.Length)] : string.Empty;
   TextBox.Text = Greet + Smile;
Dissimilis
  • 450
  • 4
  • 13
0

I'd simply generate a random double between 0, 1 (inclusive of 0 only) with random.NextDouble(). Then you can just check that a probability (as a value between 0 and 1) is less than the generated number.

For example

double chance = 0.35; //35%
bool createSmiley = rand.NextDouble() < chance;

This is not necessarily exactly precise (because of the finite precision of floating point numbers) but will get you very close. And allows for more precision than just using ints as your random numbers.

A demo of the random generation of smileys.

On another note, I'd refactor your code a bit to better isolate the logic. I'd personally just try to manipulate a single string, and set the .Text property once after all the string processing is done.

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
0

I would divide the length of your Smilies array by your probability to give a range of random numbers to generate.

4 smilies / 25% probablity = 16. So your random generator would generate a random number from 0 - 15 giving a 25% chance of using a smiley. If the generated number is outsides the bounds of the array, don't use a smiley.

using System;

public class Program
{
    public static void Main()
    {
        Random rand = new Random();
        double percentage = rand.NextDouble();

        string[] greetings = new[] { "Hi", "Hello", "Howdy!", "Hey" };
        string[] smilies = new[] { ";)", ":)", "=)", ":-)" };

        // Get a greeting
        string greet = greetings[rand.Next(0, greetings.Length)];

        // Generate the smiley index using the probablity percentage
        int randomIndex = rand.Next(0, (int)(smilies.Length / percentage));

        // Get a smiley if the generated index is within the bound of the smilies array
        string smile = randomIndex < smilies.Length ? smilies[randomIndex] : String.Empty;

        Console.WriteLine("{0} {1}", greet, smile);
    }
}

See working example here... https://dotnetfiddle.net/lmN5aP

Shar1er80
  • 9,001
  • 2
  • 20
  • 29