0

I'm trying to get an arduino to generate a random pattern in a neopixel grid every time you restart it. But I only get the same number sequence generated each time. What I'm doing wrong?

Here's the sequence that keeps happening: 1110 0000 1101 0011

Here's the code I have so far:

/* ========================== INCLUDES ================================= */
#include "RGB.h"

#include <Adafruit_NeoPixel.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_GFX.h>
#ifndef PSTR
 #define PSTR // Make Arduino Due happy
#endif

/* ========================== NEOPIXELS SETTINGS ================================= */
#define NEOPIN 6
#define MATRIX_W 4
#define MATRIX_H 4


Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(MATRIX_W, MATRIX_H, NEOPIN,
  NEO_MATRIX_BOTTOM + NEO_MATRIX_LEFT +
  NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
  NEO_GRB + NEO_KHZ800);


/* ========================== BOARD / GRID STUFF ================================= */

/* coordinates for a 4 x 4 grid
00,10,20,30
01,11,21,31
02,12,22,32
03,13,23,33,
04,14,24,34
*/

int land[4][4] = {};


/* ========================== SETUP AND LOOP ================================= */

void setup(){

  Serial.begin(9600);


  matrix.begin();
  matrix.setBrightness(60);
  matrix.fillScreen(matrix.Color(off.r,off.g,off.b));
  crossFade(off, white, 50, 5);
  drawLand();
}

void loop(){
  matrix.show();
}

/* ========================== FUNCTIONS ================================= */

  void drawLand() {
  for(int row = 0; row < MATRIX_W; row++) {
    for(int column = 0; column < MATRIX_H; column++) {

     land[row][column] = random(0,2);

     Serial.println(land[row][column]);
     if(land[row][column] == 1) {
       matrix.drawPixel(column, row, matrix.Color(red.r,red.g,red.b));
     }
   }
  }
}
miss_n
  • 101
  • 3

3 Answers3

1

Re: previous comments. You can use a product, sum, and modulus/LSB of analogRead's to make it unpredictable and give somewhat equally probable outcomes. The timing of multiple analogRead's could potentially affect the randomness.

0

It's because an machine running the same program on the same data will calculate the same result. rand() is a pseudo-random generator for that reason. With the same starting value it will create the same sequence of numbers. The only way to get different outputs is to provide different inputs: You have to start rand with a different value each run. You can set initialize rand() by supplying an unsigned int to randomSeed(). But it still needs to be different from run to run for you get different outputs from those runs.

Some devices do this by reading a real-time clock on startup and hashing the result in some way. Another method is to ask the user to press a button or a key and using the response time (and possibly the key, if it's a keyboard) to generate the seed. Counting non-repeating events, like electrical noise or background radiation is another way.

But bottom line: you need different inputs to get different outputs.

JRobert
  • 15,407
  • 3
  • 24
  • 51
0

randomSeed(analogRead(A0)); does the trick well

Better yet, use the lsb of analogreads to seed the random number generator. No repeat patterns any more.

dannyf
  • 2,813
  • 11
  • 13