PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

54 líneas
1.4KB

  1. // FILE: RandomCardDraw.ino
  2. // AUTHOR: Rob Tillaart and Walter Anderson
  3. // VERSION: 1.0.0
  4. // PURPOSE: generate random sequence (optimized)
  5. // DATE: April 24, 2014
  6. // URL:
  7. // The Entropy library provides true random numbers and can be obtained from:
  8. // http://code.google.com/p/avr-hardware-random-number-generation/wiki/WikiAVRentropy
  9. #include <Entropy.h>
  10. const byte MAX_VALUE=10;
  11. int RandomDraw[MAX_VALUE+1];
  12. byte tmp;
  13. // The following function swaps the contents of an array at the locations a & b
  14. void swap(int a, int b)
  15. {
  16. int t = RandomDraw[a];
  17. RandomDraw[a] = RandomDraw[b];
  18. RandomDraw[b] = t;
  19. }
  20. void setup() {
  21. Serial.begin(9600);
  22. // Initialize array with the sequence of values we will randomly draw from
  23. for (int i=0; i<=MAX_VALUE; i++) {
  24. RandomDraw[i] = i;
  25. }
  26. // Initialize the Entropy library to ensure that the draws are truely random.
  27. Entropy.Initialize();
  28. Serial.print("Starting to draw random sequences from 1 to ");
  29. Serial.println(MAX_VALUE);
  30. }
  31. void loop()
  32. {
  33. // fetch MAX_VALUE random numbers from the sequence 1 .. MAX_VALUE with no repeats and no missed values
  34. for (int i=1; i<=MAX_VALUE; ++i)
  35. {
  36. tmp = Entropy.random(1, (MAX_VALUE+1)-i); // select from a decreasing set
  37. Serial.print(RandomDraw[tmp]);
  38. if (i < MAX_VALUE)
  39. Serial.print(",");
  40. swap(tmp, (MAX_VALUE+1)-i); // switch the chosen one with the last of the selection set.
  41. }
  42. Serial.println(" ");
  43. }