I would like to ask how are random GUIDs and Random numbers generated under .NET Compact Framework? I was under the impression that they are using the clock to generate their randomness, but I did some experiment and it doesn't seem that they are using the clock to generate their randomness. This is the code that i used.
Random r = new Random();
string strGuid = Guid.NewGuid().ToString();
int intRandom = r.Next();
DateTime dt = DateTime.Now;
I ran them 4 times on our device running .NET compact framework 2.0. (Each test are more than 10 seconds apart). I also did something unthinkable for this experiment: on the last two tests, I removed the connection of the crystal oscillator 32.768KHz on the RTC to freeze the time intentionally and see if it can still generate randomness (I removed the RTC, soldered and placed them on a blank PCB board with jumpers and short wires). Note: I believe, you will not be able to reproduce this on an emulator or PC side due to the removal of the crystal oscillator on the RTC.
Test 1
dt {1/8/2016 10:41:14 AM} System.DateTime
intRandom 765865814 int
strGuid "63c6117b-9529-427f-822c-1ebc51089b5b" string
Test 2
dt {1/8/2016 10:47:19 AM} System.DateTime
intRandom 203285329 int
strGuid "2f84ca06-497d-4457-9def-1cf59abb4ca0" string
Test 3
dt {1/8/2016 10:47:52 AM} System.DateTime
intRandom 1935598076 int
strGuid "5ea1fe9e-0059-4bdb-b3ad-e1197e4b8d9e" string
Test 4
dt {1/8/2016 10:47:52 AM} System.DateTime
intRandom 1481224450 int
strGuid "4123b229-998c-4aeb-b90c-dd64db145b23" string
On test 1, the crystal oscillator is still connected. So far everything is ok.
On test 2, the crystal oscillator is still connected. The datetime is moving. So far, everything ok. After this test, the crystal oscillator's connection was removed. So far everything is ok.
On test 3, the crystal oscillator is disconnected before starting the test. So far everything is ok but do take note of the time.
On test 4, as expected, the timestamp is the same with test 3 due to the removal of the connection to the crystal oscillator. But the GUIDs and Random number generated are still random.
I repeated this experiment for 3 times already to ensure that I did nothing wrong. With this, how could it be that the GUIDs and the Random number are still random, despite the stoppage of the clock?
Aucun commentaire:
Enregistrer un commentaire