0

I'm trying to make a random item picker, that randomly picks items 3 by 3. The probabilities are not equal.

namespace Picker
{
    class Program
    {
        static bool doContinue = true;
        static double[] numbers = { 4, 12, 28, 60, 124, 252, 508, 1020, 2044, 4092, 4120, 4176, 4288, 4512, 4960, 5856, 7648, 11232, 18400, 32736 };

        static string Pick()
        {
            double rnd = new Random().NextDouble() * 32736;
            byte type = 0;

            for (byte i = 0; i < 20; i++) {
                if (rnd <= numbers[i])
                {
                    type = i;
                    break;
                }
            }

            string reverString = "";

            if (type < 10)
            {
                reverString = " Reverse";
            }

            return "T" + (10 - (type % 10)).ToString() + reverString;
        }

        static void Main(string[] args)
        {
            while (doContinue)
            {
                Console.WriteLine(Pick());
                Thread.Sleep(2);
                Console.WriteLine(Pick());
                Thread.Sleep(2);
                Console.WriteLine(Pick());
                Thread.Sleep(2);

                Console.ReadLine();
            }
        }
    }
}

The picks done 3 by 3 are always the same thing 3 times, and then it changes.

PS : I know, the doContinue thing is not implemented.

PearlSek
  • 90
  • 9
  • When using a random number generator, it will output the same random numbers in the same order every time it's initialized iirc. – Xander Luciano Jun 29 '16 at 18:56
  • every time you call `new Random()`, the sequence starts from the beginning. create that object only once, outside the `Pick` function. – muratgu Jun 29 '16 at 18:58
  • Try `Thread.Sleep(100);`, this will genereat enough difference in time for the seed to be different., you should be able to see the difference with value around `15` or above. – Habib Jun 29 '16 at 18:58
  • @xan: No it only does that if you feed it a number. But always sleeping for the same time will also mess the randomness. – TaW Jun 29 '16 at 18:58
  • @Hab: No, he should instead only use __one__ class level random object!! – TaW Jun 29 '16 at 18:59
  • @TaW, yes, that should fix the issue **and is the proper way*, , I am only pointing to the reason current code is behaving, since `Thread.Sleep(2);` isn't going to modify the seed, it is returning the same number. – Habib Jun 29 '16 at 19:00

1 Answers1

0

A Random object seeds itself based on the current machine time, so if you create a new Random object every time you call Pick() it will produce the same number most times that it's called in rapid succession.

Try reusing a Random instance, like this:

class Program
{
    static bool doContinue = true;
    static double[] numbers = { 4, 12, 28, 60, 124, 252, 508, 1020, 2044, 4092, 4120, 4176, 4288, 4512, 4960, 5856, 7648, 11232, 18400, 32736 };
    static Random random = new Random();
    static string Pick()
    {
        double rnd = random.NextDouble() * 32736;
        ...
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315