I have started a wireless sensor network simulation code but I don't understand the meaning of the seed and what is the return value of System.DateTime.Now.Ticks in the method below.
public void Reset(bool bNewSeed) {
// this function resets the network so that a new simulation can be run - can either be reset with a new seed, or with the previous seed (for replay.)
this.iProcessTime = 0;
this.iPacketsDelivered = 0;
foreach (WirelessSensor sensor in aSensors) {
sensor.iResidualEnergy = sensor.iInitialEnergy;
sensor.aPackets = new ArrayList();
sensor.iSensorRadius = iSensorRadius;
sensor.iSensorDelay = 0;
foreach (WirelessSensorConnection connection in sensor.aConnections) {
connection.iTransmitting = 0;
connection.packet = null;
}
}
aRadar = new ArrayList();
if (bDirectedRouting == true)
SetRoutingInformation();
iLastUpdated = iUpdateDelay;
if (bNewSeed == true)
this.iSeed = (int) System.DateTime.Now.Ticks;
r = new Random(iSeed);
}
DateTime.Now.Ticks returns a long which represents the number of ticks in that instance.
By providing a seed value to an instance of Random you are specifying the number used to calculate a starting value for the pseudo-random number sequence.
So if you have the 2 instances of Random both with the same seed they will generate the same value e.g.:
var randomOne = new Random(1);
var randomTwo = new Random(1);
var valOne = randomOne.Next(1, 1000);
var valTwo = randomTwo.Next(1, 1000);
valOne.Equals(valTwo); // True
So in order to make a random instance more random one can use a value which is less likely to be predictable, in your case the number of ticks on the instance of DateTime e.g.
var random = new Random((int)DateTime.UtcNow.Ticks);
or a much better method is:
var random = new Random(Guid.NewGuid().GetHashCode());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With