Random give the same value

hi,

i'm building a small game like puzzle so i need to swap an array items in different order each time i run my program,  every time i use random it works and make the array order diferent but the problem is every time it give the same result as if i used fixed number here its example of my code in


class Program

{
static int[] Numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
static void Main(string[] args)
{
Random rnd = new Random(0);
int i, j, tmp;
for (i = 0; i < Numbers.Length; i++)
{
// the problem in this part

j = rnd.Next(0,8);
tmp = Numbers[ i ];
Numbers[ i ] = Numbers[ j ];
Numbers[ j ] = tmp;
}
//show the result

for ( i = 0; i < Numbers.Length; i++)
{
Console.WriteLine("item[{0}] = '{1}'",i,Numbers[ i ]);
}
}
}


 

the result will be the same every time you run this, even though it  use random even if i make it other method and called it 2 times it change the order correctly first result not like second result, but if i run my progam again and did 2 trials (first time > first trial exactly the same like second time > first trial , and the second trial is the same too)

i want to do this in different way every time i run the program or the random generator

is there anyway to do that

thx in advance




Answer this question

Random give the same value

  • peter_heard01

    The argument you are passing to Random constructor is seed. IOW if you pass same value you will get same sequence.

    If you want more random behaviour then you shouldn't pass any value, like:

    Random rnd = new Random();

    or you should pass a random value you get from somewhere.



  • Random give the same value