For random values, use the class
System.Random
; you need to use its method
Next(int maxValue)
; see
http://msdn.microsoft.com/en-us/library/system.random.aspx[
^].
(I hate to note this, but abnormally high number of beginners make totally idiotic mistake which you should not repeat: they create an instance of the class
System.Random
in cycle, which is a source of big problems. The instance of this class should be created only once.)
I guess you need to measure time of certain calculations. For this purpose, do not use anything but the class
System.Diagnostics.Stopwatch
, see
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[
^].
This is the most accurate tool available. You can learn what accuracy you can have using the static properties
System.Diagnostics.Stopwatch.Frequency
and
System.Diagnostics.Stopwatch.IsHighResolution
.
The property
Elapsed
returns the value of the type
System.TimeSpan
. You should better use
double
properties of this type to obtain the real-time values in fractional seconds, milliseconds, etc., using its properties
System.TimeSpan.TotalSeconds
,
System.TimeSpan.TotalMilliseconds
, etc. See
http://msdn.microsoft.com/en-us/library/system.timespan.aspx[
^].
—SA