-1

Below is the code I used to declare the variables:

Random x= new Random();
int xval;
string xresult;

xval = x.Next(4);
string[] xlist = {
    "ZERO",
    "ONE",
    "TWO",
    "THREE",
};
xresult = xlist[xval];

My problem is that the result is the same for all the times I used it. I want it to change or reset each time.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jack
  • 1

2 Answers2

0

Random as you use it is initialized with a seed derived from your DateTime.Now - you use it to generate a number between 0 and 4 (exluded) - so 25% of the time you'll get the same number:

using System;
using System.Collections.Generic;

public class Program
{    
    public static void Main()
    {
        Random x= new Random();

        List<int> ints = new List<int>();

        for(int i=0; i< 100; i++) ints.Add(x.Next(4));

        Console.WriteLine(string.Join(", ",ints));
    }
}

Output (reformatted):

3, 0, 1, 1, 1, 1, 3, 0, 2, 0, 1, 2, 1, 0, 3, 1, 3, 0, 2, 2, 3, 1, 1, 2, 1, 
2, 3, 3, 2, 3, 3, 2, 0, 3, 3, 3, 0, 0, 3, 0, 2, 1, 3, 3, 1, 0, 2, 1, 3, 0,
2, 2, 3, 3, 1, 1, 2, 3, 3, 2, 2, 0, 2, 2, 1, 2, 2, 1, 1, 3, 1, 3, 1, 2, 0,
2, 2, 1, 0, 1, 3, 1, 0, 1, 1, 1, 2, 0, 2, 1, 1, 0, 0, 0, 1, 2, 3, 0, 2, 2

It produces different ones. You were just un-lucky.

You should move the declaration Random x=new Random() (badname btw) outside where you use it, so you do not create it over and over again, maybe getting the similar runs for "different" seeds based on your time. You can have the same 100 digits in different seeded runs of Random (by chance). Random does not mean different, especially if you only use 4 values to mapp all your randomness to.

After the remark in the comment - Illustrate "same" starting sequences for different random seeds:

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{    

    static IEnumerable<int> FirstNumbersFromSeed(int seed, int num)
    {
        var r = new Random(seed);
        for (int i=0;i<num;i++)
            yield return r.Next(4);
    }

    public static void Main()
    {

        Dictionary<string,List<int>> dict = new Dictionary<string,List<int>>();

        int num = 5;
        for (int i =0;i<2000; i++)
        {
            var key = string.Join(",",FirstNumbersFromSeed(i,num));

            if (dict.ContainsKey(key)) dict[key].Add(i);
            else dict[key] = new List<int>(i);
        }

        foreach(var kvp in dict.Where(d => d.Value.Count > 1))
            Console.WriteLine(string.Join(", ",kvp.Key) + " same first " + num + " values for seed with ints: " + string.Join(",",kvp.Value));
    }
}

Output:

2,3,3,2,0 same first 5 values for seed with ints: 239,890
0,0,1,3,2 same first 5 values for seed with ints: 1463,1576
3,1,0,3,0 same first 5 values for seed with ints: 540,653,1876
1,1,1,2,1 same first 5 values for seed with ints: 656,769,895
<snipped 300  more output lines>
1,0,1,3,3 same first 5 values for seed with ints: 1862,1988

So you have about 305 "collisions" which deliver the same 5 starting "random" values for seeds between 0 and 2000 if you draw random values from 0 to 4(exclusive).

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 1
    You are making assumptions about how they are using it. Probably they are getting the issue in the dupe. – Martin Smith Dec 30 '17 at 14:21
  • @MartinSmith added code to show "same" sequences from random using different seeds and producing same first 5 random numbers in sequence – Patrick Artner Dec 30 '17 at 14:44
0

If you just want to extract a random value from your array, use the following code:

private static Random s_Random = new Random();

public static void Main(string[] args)
{
    String[] xlist =
    {
        "ZERO",
        "ONE",
        "TWO",
        "THREE"
    };

    Int32 idx;

    for (Int32 i = 0; i < 10; ++i)
    {
        idx = s_Random.Next(xlist.Length);
        Console.WriteLine(xlist[idx]);
    }
}

Note that it very important to create a unique Random instance and reuse it every time a random number is needed in order to achieve uniformity in generated numbers.

You can try a working demo here.

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98