Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Seed_PRNG.ino 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Use Entropy library functionality to seed the avr-libc pseudo-
  2. // random number generator
  3. //
  4. // Entropy - A entropy (random number) generator for the Arduino
  5. //
  6. // Copyright 2012 by Walter Anderson
  7. //
  8. // This file is part of Entropy, an Arduino library.
  9. // Entropy is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // Entropy is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with Entropy. If not, see <http://www.gnu.org/licenses/>.
  21. #include <Entropy.h>
  22. // The following addresses a problem in version 1.0.5 and earlier of the
  23. // Arduino IDE that prevents randomSeed from working properly.
  24. // https://github.com/arduino/Arduino/issues/575
  25. #define randomSeed(s) srandom(s)
  26. void setup()
  27. {
  28. uint32_t seed_value;
  29. Serial.begin(115200);
  30. while (!Serial) {
  31. ; // wait for serial port to connect. Needed for Leonardo and Due
  32. }
  33. Entropy.Initialize();
  34. // This routine sets up the watch dog timer with interrupt handler to maintain a
  35. // pool of real entropy for use in sketches. This mechanism is relatively slow
  36. // since it will only produce a little less than two 32-bit random values per
  37. // second.
  38. Entropy.Initialize();
  39. // The random method returns an unsigned 32-bit value, which can be cast as a
  40. // signed value if needed. The function will wait until sufficient entropy is
  41. // available to return, which could cause delays of up to approximately 500ms
  42. seed_value = Entropy.random();
  43. Serial.print("Seed value = ");
  44. Serial.println(seed_value);
  45. // By using the Entropy library to seed the normal pseudo-random number generator which
  46. // ensures that the standard libraries random number generator will provide different starting
  47. // values each time the sketch is run. This performs much better than the normal
  48. // randomSeed(analogRead(0)).
  49. randomSeed(seed_value);
  50. // Here are some typical values produced when this sketch was run 25 times
  51. // Seed value = 2054931336
  52. // Seed value = 2689566361
  53. // Seed value = 953766268
  54. // Seed value = 1799328995
  55. // Seed value = 3050792285
  56. // Seed value = 607814576
  57. // Seed value = 2314140972
  58. // Seed value = 1573721872
  59. // Seed value = 507815617
  60. // Seed value = 1678088733
  61. // Seed value = 2655736882
  62. // Seed value = 2307754733
  63. // Seed value = 2704765785
  64. // Seed value = 2924991904
  65. // Seed value = 823689487
  66. // Seed value = 3858651144
  67. // Seed value = 183355835
  68. // Seed value = 1792358414
  69. // Seed value = 4175350052
  70. // Seed value = 467439649
  71. // Seed value = 1884043255
  72. // Seed value = 3786687950
  73. // Seed value = 1349957766
  74. // Seed value = 652610269
  75. //
  76. // The more normal randomSeed(analogRead(0)) produces far less random seed
  77. // values as showm in the following 25 examples:
  78. // Seed value = 303
  79. // Seed value = 326
  80. // Seed value = 327
  81. // Seed value = 326
  82. // Seed value = 326
  83. // Seed value = 328
  84. // Seed value = 328
  85. // Seed value = 328
  86. // Seed value = 330
  87. // Seed value = 328
  88. // Seed value = 328
  89. // Seed value = 329
  90. // Seed value = 327
  91. // Seed value = 328
  92. // Seed value = 328
  93. // Seed value = 329
  94. // Seed value = 329
  95. // Seed value = 329
  96. // Seed value = 331
  97. // Seed value = 331
  98. // Seed value = 330
  99. // Seed value = 331
  100. // Seed value = 329
  101. // Seed value = 329
  102. // Seed value = 329
  103. }
  104. void loop()
  105. {
  106. }