views:

2699

answers:

3

What is the c# equivalent of the following c++:

srand((unsigned)(time(NULL)));
weight=(double)(rand())/(RAND_MAX/2) - 1;
+1  A: 

To do random value generation in .NET, you should use the Random class. to seed it with a time value, use: Random rand = new Random((int)DateTime.Now.Ticks);

For further specifics, it's best to check out the docs about the Random class in the MSDN, e.g. which methods are available.

Frans Bouma
+1  A: 
Random rnd = new Random((int)DateTime.Now.Ticks);
return rnd.Next(-1,1);
schnaader
+1  A: 

The paramaterless constructor for Random uses "a time-dependent default seed value" so all you need is:

Random rnd = new Random();
return rnd.Next(-1, 1);
ICR