Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm new to coding, and just working on a little project to learn.

I'm trying to produce multiple different versions of sets of attributes that I'll be inserting into a SQLite database.

My current code prints the same numbers each time, ie:

C#
7
2
12
13
7
2
12
13

... etc.

I guess it has to do with the constructor loop? Anyway I can't figure out why it is happening or how to solve it, any help or explanation would be greatly appreciated.

Thanks!

What I have tried:

C#
            for (int i = 0; i < 10; i++)
            {
                GenData gd = new GenData();
                gd.GenStats();
            }

//Above in method in different class to below.

    class GenData
    {
        
        public void GenStats()
        {

            Random rand = new Random();

            int[] arr = new int[4];

            for (int x = 0; x < arr.Length; x++)
            {

//Weighted probability of values

                if (rand.NextDouble() < 0.90)
                {
                    arr[x] = rand.Next(1, 15);
                }
                else
                {
                    arr[x] = rand.Next(1, 20);
                }
            }

            foreach (var item in arr)
                {
                Console.WriteLine(item.ToString());
                }

            int sum = arr.Sum();

            //Console.WriteLine("Sum: " + sum + Environment.NewLine);
        }
    }
Posted
Updated 22-Feb-16 3:26am

First off, move the Random instance outside any method, and make it private:
C#
class GenData
{
    private Random rand = new Random();
    public void GenStats()
    {
That way, it isn't recreated each time you call the method, which can mean repeated values. That's probably what's causing the problem in this case, because the Random constructor uses the system time to "start the sequence" and if you call the GenStats routine quickly in succession, the time doesn't change between constructor calls because your processor is too fast!
Secondly, if you are going to build new GenData instances so quickly, that isn;t going to fix it completely either - so make the Random instance static:
C#
class GenData
{
    private static Random rand = new Random();
    public void GenStats()
    {
Normally, I wouldn't suggest that as it isn't necessarily thread safe, but for this exercise it's probably fine.
 
Share this answer
 
v2
Comments
Keith Lewis 22-Feb-16 10:15am    
Thankyou! I feel a bit silly as I did read about that issue, but maybe I didn't really understand it. Makes sense now. I also moved the GenData constructor out of the loop which seems to make a difference.
You need to seed the random generator so it starts at a different value each time. Using the clock time in milliseconds is a useful way of doing it. See Random Constructor (Int32) (System)[^].
 
Share this answer
 
Comments
Keith Lewis 22-Feb-16 10:15am    
Thankyou, I'll do some reading!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900