Weirdest C# Error...

bchip

Expert Member
Joined
Mar 12, 2013
Messages
1,324
Reaction score
418
I'm currently coding something that does the following:
List of numbers: 10, -20, 15, -5, 10 (start with 5)
Shuffle the numbers and see how many of these dip below 10 (then stop)

So possible outcomes are
1) 5 -> 10-20, stop = -5
2) 5 -> -20,stop = -15
3) 5 -> 10+10+15-20 = 15 (all got through)
etc

All the values -5, -15, 15... are capture in a List<double>

When I run the code in debug mode with a watch I get random outcomes
When I run the code without debug mode I would get -5,-5,-5,-5 (same number over and over)


To have the same number over and over is really impossible.

I think it has to do with it the timing issue in the for loop with the the time it took to save it to a List

Anyone got any ideas on this?

Code:
double ftotal = 0;
List<double> nList = GroupList.Shuffle(theList);
                    for (int tcnt = 0; tcnt < nList.Count; tcnt++)
                    {
                        if (!isRuined)
                        {
                            if (ftotal < minallwd)
                            {
                                isRuined = true;
                                ncontracts = 0;
                            }
                        }
                        ftotal += (nList[tcnt] * ncontracts);
                    }

capList.Add(ftotal);
 
Update:

When I add the following
System.Threading.Thread.Sleep(50);

The values seem more random... so its what I'm expecting.
Is there any way to tell the PC to wait before saving?


Edit:
When I change it to 50 milliseconds works
When I change it to 10 milliseconds every 2nd one is still doubled
When I change it to 30 milliseconds works perfectly...but adds 2 min to the calcs :(
 
Last edited:
Check on your shuffle method, it's probably not initialising a proper randomiser method.

Thanks for the response.

I use a basic shuffle method:
Not sure if this should be something else?

Code:
  //Fisher–Yates shuffle algorithm
        public static List<double> Shuffle(List<double> aList)
        {
            List<double> newList = GroupList.CopyList(aList);
            Random random = new Random();
            int n = newList.Count;

            for (int i = newList.Count - 1; i > 1; i--)
            {
                int rnd = random.Next(i + 1);
                double value = newList[rnd];
                newList[rnd] = newList[i];
                newList[i] = value;
            }

            return newList;
        }
 
Hi, your issue is that you need to create one instance of Random which is used by your static shuffle method.

You are instantiating Random every time you call your shuffle method, a value based on system time is used for the seed by default, which because of the system time being so close together (because debug loads additional symbols and quite a few things to allow you to step through the code, break on exceptions etc.) is using the same seed to initialize the random generator (you can pass in a long value as a seed to the constructor if you want deterministic behaviour for each time your program runs) will always produce the same set of random numbers for example, i.e. PRNGs aren't really random). It's possible that if you did more work you'd find the numbers would change, or as you've found sleeping the thread changes the time used for each instantiation of the random generator.
 
Hi, your issue is that you need to create one instance of Random which is used by your static shuffle method.

You are instantiating Random every time you call your shuffle method, a value based on system time is used for the seed by default, which because of the system time being so close together (because debug loads additional symbols and quite a few things to allow you to step through the code, break on exceptions etc.) is using the same seed to initialize
...

Awesome!

Thanks it seems to work.
 
Top
Sign up to the MyBroadband newsletter
X