Random method generates the same numbers

Hi, I just got a crow and was running samplehold.lua from the Bowery. I added a print method to see what numbers are being generated. I noticed the random method is repeating the same numbers over and over:

-3.530273                                                                                                                                                                                               
-2.508908                                                                                                                                                                                                 
-0.5078893                                                                                                                                                                                                
-3.208938                                                                                                                                                                                                 
0.8197746                                                                                                                                                                                                 
-3.656103                                                                                                                                                                                                 
-3.936201                                                                                                                                                                                                 
-0.9434314                                                                                                                                                                                                
2.379559                                                                                                                                                                                                  
3.412273                                                                                                                                                                                                  
3.541624                                                                                                                                                                                                  
3.660694                                                                                                                                                                                                  
-2.06203                                                                                                                                                                                                  
-0.01668262                                                                                                                                                                                               
0.1060004                                                                                                                                                                                                 
0.16396                                                                                                                                                                                                   
-3.530273 (first repetition...)                                                                                                                                                                                               
-2.508908                                                                                                                                                                                                 
-0.5078893                                                                                                                                                                                                
-3.208938                                                                                                                                                                                                 
0.8197746                                                                                                                                                                                                 
-3.656103                                                                                                                                                                                                 
-3.936201                                                                                                                                                                                                 
-0.9434314                                                                                                                                                                                                
2.379559                                                                                                                                                                                                  
3.412273                                                                                                                                                                                                  
3.541624                                                                                                                                                                                                  
3.660694                                                                                                                                                                                                  
-2.06203                                                                                                                                                                                                  
-0.01668262                                                                                                                                                                                               
0.1060004                                                                                                                                                                                                 
0.16396                                                                                                                                                                                                   
-3.530273  (second repetition...) etc.

Is this expected behaviour? Is there a way to make it more random?

seems some kind of bug to me… on crow, math.random() ultimately calls down into the STM32 HAL’s RNG module, which is driven by analog noise (albeit through a linear shift register.) so there is no PRNG or seed problem.

but the road from the HAL the lua function is a little twisty… i’d leave it to @Galapagoose to diagnose.

in the meantime, for comparison, it would be interesting to compare with the integer version, something like math.random(10000)/5000 - 1

Tried as you suggested, result:

-0.4326                                                                                                                                                                                                   
-0.4816                                                                                                                                                                                                   
-0.3684                                                                                                                                                                                                   
0.8232                                                                                                                                                                                                    
-0.1824                                                                                                                                                                                                   
0.216                                                                                                                                                                                                     
-0.7674                                                                                                                                                                                                   
0.2452                                                                                                                                                                                                    
-0.4326 (repeats)                                                                                                                                                                                                   
-0.4816                                                                                                                                                                                                   
-0.3684                                                                                                                                                                                                   
0.8232                                                                                                                                                                                                    
-0.1824                                                                                                                                                                                                   
0.216                                                                                                                                                                                                     
-0.7674                                                                                                                                                                                                   
0.2452                                                                                                                                                                                                    
-0.4326 (repeats)                                                                                                                                                                                                  
-0.4816                                                                                                                                                                                                   
-0.3684                                                                                                                                                                                                   
0.8232                                                                                                                                                                                                    
-0.1824                                                                                                                                                                                                   
0.216                                                                                                                                                                                                     
-0.7674                                                                                                                                                                                                   
0.2452                                                                                                                                                                                                    
-0.4326  (repeats)                                                                                                                                                                                                 
-0.4816                                                                                                                                                                                                   
-0.3684                                                                                                                                                                                                   
0.8232                                                                                                                                                                                                    
-0.1824                                                                                                                                                                                                   
0.216                                                                                                                                                                                                     
-0.7674                                                                                                                                                                                                   
0.2452                                                                                                                                                                                                    
-0.4326  (repeats..)                                                                                                                                                                                                 
-0.4816

Reality is experiencing longer than average wait times due to high load on the system. To avoid reducing the temporal resolution of your simulation instance, some features of quantum mechanics have been disabled locally. We apologize for the inconvenience.

:telephone_receiver: :speech_balloon:

24 Likes

liked this quote and interested where it is from.

2 Likes

@etsuko
I’ve just tested it on a number of crows here and haven’t reproduced the issue. The first thing I’d suggest is to simply restart crow and see if that solves the problem. I assume this will solve the problem and it should be very rare that it will happen again.

On a technical level..

We use the analog random number generator built into the STM microcontroller (as @zebra noted). Because requesting a new random value is not instantaneous, I implemented a queue of 16 random values which will replenish itself in the background once 12 of the values have been used.

This effectively means you can’t use more than 16 random values in a single event callback (or you’ll see repetitions). So running for i=1,20 do print( math.random() ) end the last 4 ‘random’ numbers will be the same as the first 4.

EDIT: I just tested this and even the above line prints 20 genuinely random values somehow

That said, if you’re just using the sampleandhold.lua bowery script, this is not happening and everything should work correctly with no discernible pattern. What I imagine is happening is that the RNG on the microcontroller is getting into a seed-error state which requires it to be reset which the current code does not handle. AFAIK this should be exceptionally rare occurance, and restarting the processor should solve the issue.

It’s not terribly difficult to fix this, but it is very difficult to test whether the ‘fix’ has worked. I’ve added it to the issue tracker to at least make a basic effort to restart the RNG if the error event occurs.

All this said, if the issue persists for you even after power-cycling the case, it suggests my understanding of the issue is wrong & I will think about what else could be causing this…

3 Likes

My brain, likely channeling some SCP entries I’ve been reading lately…

2 Likes

The problem persists after a power cycle; the second example I posted was already after a power cycle, and I just tried it once more to be sure.

Good news, it works after downgrading firmware.

I was on 2.1.1 for all the tests I shared in this thread. I downgraded to 2.0.0 just now, but the problem persisted. Then downgraded to 1.0.3, and the issue disappeared! Then upgraded to 2.0.0 again, and the issue is gone there too :man_shrugging: I’m quite interested in what might have ever caused this, but mostly happy the quantum mechanics have been enabled locally again.