Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Teensy4.x Entropy library documentation?

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    12

    Teensy4.x Entropy library documentation?

    I'm trying to use a random number generator for an audio sequencer using the latest Entropy library and a Teensy 4.0. Everything compiles and appears to work, but simply using Entropy.random(0,15); for a 16-step sequence appears to repeat every 64 numbers. Does this library use a seed function? How should this library be used to improve the randomness, assuming I'm looking for uniform distribution? Also, it looks like there's a normal distribution method Entropy.rnorm(mean, stdv) in there as well, which could be fun to use! But I can't seem to find the documentation for the latest library. Does anyone have some links that could help educate me on the use of the newest Entropy library for a T4.x or at least show me where to find the header file (I'm using PlatformIO)? I read something about a hardware true random number generator which I would love to play around with! Thanks for any tips!

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,513
    Quote Originally Posted by Slabshaft View Post
    but simply using Entropy.random(0,15); for a 16-step sequence appears to repeat every 64 numbers.
    I tried to reproduce this problem with the following code.

    Code:
    #include <Entropy.h>
    
    void setup() {
      Entropy.Initialize();
    }
    
    void loop() {
      for (int i=0; i < 64; i++) {
        Serial.print(Entropy.random(0, 15));
        Serial.print(",");
      }
      Serial.println();
    }
    Here's the result I see in the serial monitor.

    Click image for larger version. 

Name:	sc.jpg 
Views:	13 
Size:	255.1 KB 
ID:	21047

    Every line of 64 numbers looks very different to me. It also generates very different data each time I run it.


    Does this library use a seed function? How should this library be used to improve the randomness, assuming I'm looking for uniform distribution?
    Entropy uses the hardware random number generator, so you are supposed to get truly random numbers.

    Any chance you forgot to use Entropy.Initialize(); in your setup() function?

    If it still won't work, please try running that tiny test program with Arduino. If it works in Arduino but not in PlatformIO, maybe report the issue to PlatformIO.

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    12
    Yep, I was missing the Entropy.Initialize() and it's working perfectly. Thanks!

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,223
    Just for ref - playing with Multi Serial did a count of Entropy results for values :: Entropy.random(0, 15)
    This is output from SerialUSB1 - Let it run on two T_4.1's let it run for 1 second and print the first number on each row for the second - then add those to the full run accumulation:
    Code:
    ------------
    81	9983411
    73	9987762
    74	9982774
    94	9985073
    83	9987729
    86	9987859
    73	9988031
    84	9986454
    86	9990200
    99	9982778
    72	9982688
    83	9984883
    68	9985951
    82	9980222
    78	9983961
    and second T_4.1:
    Code:
    ------------
    75	9954582
    77	9956504
    86	9953401
    81	9953565
    74	9953762
    93	9960052
    82	9949195
    77	9956079
    78	9957814
    89	9955721
    77	9955418
    69	9955837
    76	9954869
    102	9956544
    80	9954209

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •