-1

This code gives out a random output each time, but for somereason the output is very similar: This is the code:

using System;
using System.Collections.Generic;
namespace dimensionrandomwalk
{
    class MainClass
    {
        public static void Main (string[] args){

            for(int p = 0; p<100; p++){
        {
            Random rnd = new Random();
            int[] x;
            x = new int[500];
            for (int i = 0; i < 500; i++){
                int L = rnd.Next (0, 2);

                x[i] = (L==0) ? -1 : 1;
            }

            int total_value = 0;
            for (int i = 0; i < 500; i++)
                 total_value += x[i];

                 Console.WriteLine ("Total: " + total_value);
                 }
     }
    }
 }
}

and this is the output: http://gyazo.com/a0f11edd17eff6dde8523ac4dbf19629.png

Help please.

  • 4
    Initialize `Random` outside of the loop. See e.g. [this SO posting](http://stackoverflow.com/questions/5398336/random-number-generator-always-picks-the-same-value-when-run-inside-a-loop). – Uwe Keim Nov 08 '13 at 11:02
  • @Uwe Keim : Now the output is: http://pastebin.com/z92nh4t4 doesn't seem 100% random – user2968068 Nov 08 '13 at 11:08
  • Because `Random` is psuedo-random. Why don't you narrow down the issue? You pasted a lot of code, which we will have to read and interpret to guess what you expect it to do. We also don't know whether it does that properly. Explain what each bit of code is supposed to do, or even better, extract the relevant code and demonstrate and explain the actual problem. – CodeCaster Nov 08 '13 at 11:10
  • @ CodeCaster: If you isolate the code to this http://pastebin.com/6c8BVgSw then it works exactly randomly as I want. But I want this code to be executed 100 times. – user2968068 Nov 08 '13 at 11:13
  • 1
    Of course if you call it once it seems random. [Four is also random](http://xkcd.com/221/). The logic you programmed makes the results a lot less differentiating from each other when called multiple times, see my answer why. – CodeCaster Nov 08 '13 at 11:24

3 Answers3

3

Jon Skeet explains this problem in this article of his. The core problem is this:

If you start off an instance of Random with the same initial state (which can be provided via a seed) and make the same sequence of method calls on it, you'll get the same results.

So what was wrong in our example code? We were using a new instance of Random on each iteration of the loop. The parameterless constructor for Random takes the current date and time as the seed [...]

Community
  • 1
  • 1
germi
  • 4,628
  • 1
  • 21
  • 38
3

new Random() creates a new Random object seeded based on time. Since the loop will run many many times per second, it's not actually changing. Instead, you should create the Random object once and call the same object over and over, which will move through the table instead of taking the same value, giving you the pseudo-random values you want.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Tobberoth
  • 9,327
  • 2
  • 19
  • 17
  • ok how should I do that? By having the Random rnd = new Random() outside the loop? – user2968068 Nov 08 '13 at 17:57
  • Right. Instantiate a random object as a global variable and then always reference the global variable. By only seeding it once and then calling it over and over, you'll move through a table of pseudo-random numbers. It's about as random as you can get something in a computer program, unless you use the API from random.org. – Tobberoth Nov 08 '13 at 20:50
1

You don't explain what you want this code to do. It seems like you want to generate 500 times either -1 or +1 and at the end add all 500 numbers.

I don't know why you are doing that, as it will have many situations that return the same value while the arrays-of-500 contains differences. Compare (simplified to 5 elements instead of 500):

{ +1, +1, -1, -1, +1 },
{ +1, -1, +1, -1, +1 },
{ -1, +1, -1, +1, +1 },
{ -1, -1, +1, +1, +1 }

All four will return +1 when added up in the loop where you calculate total_value. So no matter how many times you call this, the results won't differ that much.

Perhaps if you explain what your ultimate goal is, you can get better answers.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272