-2

Possible Duplicate:
Random number generator not working the way I had planned (C#)

I have LinkedList of LinkedList, and when I try to dispaly after filling, the raws are the same, but the most curious is that in debug mode it dispalays different rows. BTW I have VS11 beta.

    private LinkedList<LinkedList<int>> grid = new LinkedList<LinkedList<int>>();


    public void CreateMatrix(int rows, int coloumns, int maxSize)
    {
        for(int i = 0; i < rows;i++)
        {
            grid.AddFirst(generateList(coloumns, maxSize));
        }
    }



    private LinkedList<int> generateList(int size, int maxSize)
    {
        var ranodGenerator = new Random();
        var list = new LinkedList<int>();
        for (int j = 0; j < size; j++)
        {
            list.AddFirst(ranodGenerator.Next(maxSize));
        }
        return list;
    }


    public void DisplayMatrix()
    {
        foreach (var list in grid)
        {
            foreach (var i in list)
            {
                Console.Write(i+ " ");
            }
            Console.WriteLine();
        }
    }

So after

MatrixManager matrixManager = new MatrixManager();
            matrixManager.CreateMatrix(4,4,200);
            matrixManager.DisplayMatrix();

it will display 4 identical rows

134 3 45 26
134 3 45 26
134 3 45 26
134 3 45 26

but should display different

Community
  • 1
  • 1
jjjojjjooo
  • 23
  • 8
  • Focus your question. What are you experiencing, specifically, and what do you expect to happen? – Michael Petrotta Apr 14 '12 at 01:40
  • Is _raws_ some new type from C#5 that I've overlooked? Or do you really mean **rows** (which still doesn't make much sense to me)? – M.Babcock Apr 14 '12 at 01:41
  • My best guess is that he means ROWS, which would be what he uses to reference a list member. – Mathieu Apr 14 '12 at 01:42
  • @Mathieu - Perhaps but linked lists don't really have _rows_, they have elements. – M.Babcock Apr 14 '12 at 01:43
  • I'm sory of course it's rows, I mean list elements – jjjojjjooo Apr 14 '12 at 01:44
  • Any particular reason you don't just use a simple int[][] ? – asawyer Apr 14 '12 at 01:46
  • I have to delete some rows and columns and it's not possible to achive using arrays – jjjojjjooo Apr 14 '12 at 01:48
  • 2
    So if you take the normal to the anti-relative neutron core phase structure and cross it pairwise with the components of the left adjunct beta emmision field, you'll obtain a linked coupling of raw field flux energy. Record the entries in a spatial coefficient list and display the row-wise exclusive entries arising from a maximum cut in the Fresnel-Briggs graph. – dcow Apr 14 '12 at 01:59

2 Answers2

1

This should be duplicate of "How to use Random"

var ranodGenerator = new Random();

This line restarts creation of random numbers. Create one random number generator and life will be much better.

Here is the FAQ: Random number generator only generating one random number

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Thank you, it helped, but I'm still curious why in debug mode lines were different. – jjjojjjooo Apr 14 '12 at 01:53
  • @jjjojjjooo Were you stopping with break points ? – asawyer Apr 14 '12 at 01:55
  • 1
    First line of FAQ - "Every time you do new Random() it is initialized using the clock. This means that in a tight loop you get the same value lots of times. " - so less time it takes to run the code more chances to get the same values. Especially stepping through the code makes it sllloooowwwwww. – Alexei Levenkov Apr 14 '12 at 01:56
1

Move "Random ranodGenerator = new Random();" to the top of the file, and use the same one for the whole matrix.

You are creating a new Random object for each row. Creating a new Random object starts the random number generation over from the start.

Random numbers in programming are not actually random. They are generated by a mathematical formula being applied over and over to the result of the previous iteration.

CuriousGeorge
  • 7,120
  • 6
  • 42
  • 74