Arduino random numbers! - (Arduino Uno Programming for Beginners)

Поделиться
HTML-код
  • Опубликовано: 6 июл 2024
  • Arduino random numbers! - (Arduino Uno Programming for Beginners)
    In this video I show you how to generate random numbers, and not so random numbers.
    0:00 intro
    0:13 rand basics
    1:37 deterministic random numbers
    2:01 generate dice numbers
    3:19 random seed
    4:27 button exercise
    5:00 button solution
    6:10 only one number for one button press
    8:14 debouncing

Комментарии • 10

  • @Volker-Dirr
    @Volker-Dirr 4 месяца назад +1

    Nice video.
    Just some additional information:
    - Setting the random seed only once at the beginning is sometimes very useful, since it is easier to debug your code.
    - Using millis() as seed is not good if you need very many random times, since if millis() didn't change (since the Arduino is fast), you always get the same random number
    - If you need very many random numbers it is better to not always reset the seed, since that is slow.
    - Don't use milllis() if you want to code security based code like generating random numbers for encrypted communication. Because for skilled coders this will be too less random. But for "normal" use case millis() is fine.
    - very good that you noticed that analogRead is only 10 bits, but the seed should have more bits. I didn't thought fully about bugs, but i think a variant like
    randomSeed(analogRead(0)+analogRead(1)*64+analogRead(2)*4096+...) might be fine.
    A nice task is always writing a small dice checker.
    So for example throwing the dice 60000 and count how many times you get the 1, 2, 3, 4, 5 and 6.
    And then check if each number is around 10000 shown.
    Because with such an easy test you might already find bugs like setting the seed too fast and/or wrong/bad random() functions. I remember in the past guys coded the random() function by using a function that returns a float with values between 0 and 1. And while converting that float into an integer they done it wrong and by that the number 1 showed on the dice only 5000 times (instead of 10000) and the number 6 showed 15000 times (instead of 10000), because they rounded wrong.

    • @playduino
      @playduino  4 месяца назад

      Thank you very much for the valuable additional information! The dice checker is a great idea, I will definitely do this at the end of the project

    • @Volker-Dirr
      @Volker-Dirr 4 месяца назад

      @@playduino It just flashed my mind:
      Maybe also write a similar check for "analogRead(0)". So see how random that is.
      So reading it maybe 1024000 times and see how many times each number is read. I am not sure, but i guess/fear that the analogRead might not always change if you read too fast, so also with a small delay it might be better.

    • @AlexanderWeurding
      @AlexanderWeurding 4 месяца назад

      Love the share and reaction. Is there really something like a Random number;)

    • @playduino
      @playduino  4 месяца назад

      Good question, I guess no, but by using all sorts of tricks we can get very close :)

    • @AlexanderWeurding
      @AlexanderWeurding 4 месяца назад

      @@playduino verry close is the same as random. var word = translated, a new word, orfpan word etc