c# - using System.DateTime.Now.Ticks for seed value -


i have started wireless sensor network simulation code don't understand meaning of seed , return value of system.datetime.now.ticks in method below.

public void reset(bool bnewseed) {     // function resets network new simulation can run - can either reset new seed, or 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 long represents number of ticks in instance.

by providing seed value instance of random specifying number used calculate starting value pseudo-random number sequence.

so if have 2 instances of random both same seed generate 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 make random instance more random 1 can use value less predictable, in case number of ticks on instance of datetime e.g.

var random = new random((int)datetime.utcnow.ticks); 

or better method is:

var random = new random(guid.newguid().gethashcode()); 

Comments