Random(min,max) Function only giving ~50 range no matter what the values?

I can't get the min,max range to function properly. Monitoring the serial, only gives out ~50 difference from the min. I then added truerandom.ino to try to fix, but no help.

const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//long interval = random(800, 8000); // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
long interval = random(800,8000);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
Serial.println(interval);
}
}

Added truerandom.ino still ~50 range:

// TrueRandomSeed.ino
//   This example sketch shows how to provide a truly random seed value to the built in 
//   library pseudo random number generator.  This ensures that your sketch will be
//   using a different sequence of random numbers every time it runs.  Unlike the 
//   usually suggested randomSeed(analogRead(0)) this method will provide a much more 
//   uniform and varied seed value.  For more information about the basic technique used
//   here to produce a random number or if you need more than one such number you can 
//   find a library, Entropy from the following web site along with documentation of how
//   the library has been tested to provide TRUE random numbers on a variety of AVR 
//   chips and arduino environments. 
//
//   https://sites.google.com/site/astudyofentropy/project-definition/
//           timer-jitter-entropy-sources/entropy-library
//
//   Copyright 2014 by Walter Anderson, wandrson01 at gmail dot com
//

#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/atomic.h>
// The following addresses a problem in version 1.0.5 and earlier of the Arduino IDE 
// that prevents randomSeed from working properly.
//        https://github.com/arduino/Arduino/issues/575
#define randomSeed(s) srandom(s)

volatile uint32_t seed;  // These two variables can be reused in your program after the
volatile int8_t nrot;    // function CreateTrulyRandomSeed()executes in the setup() 
                         // function.
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated

void CreateTrulyRandomSeed()
{
  seed = 0;
  nrot = 32; // Must be at least 4, but more increased the uniformity of the produced 
             // seeds entropy.

  // The following five lines of code turn on the watch dog timer interrupt to create
  // the seed value
  cli();                                             
  MCUSR = 0;                                         
  _WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (1<<WDE); 
  _WD_CONTROL_REG = (1<<WDIE);                       
  sei();                                             

  while (nrot > 0);  // wait here until seed is created

  // The following five lines turn off the watch dog timer interrupt
  cli();                                             
  MCUSR = 0;                                         
  _WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (0<<WDE); 
  _WD_CONTROL_REG = (0<< WDIE);                      
  sei();                                             
}

ISR(WDT_vect)
{
  nrot--;
  seed = seed << 8;
  seed = seed ^ TCNT1L;
}

void setup()
{
  CreateTrulyRandomSeed();
  randomSeed(seed);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); 
  // The preceeding two function calls will take approximately 0.5 second to execute if 
  // nrot is set to 32 ... the rest of your setup code should FOLLOW from here.
}

void loop()
{
  long interval = random(800,8000);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
Serial.println(interval);
}
}

You are generating a new interval value each time through loop(). The test for a timeout is testing against a new value each time through loop() so will naturally trigger on low values which is the one you print. Add a Serial.print(interval); immediately after the line that generates a new interval and you will see that random() is working correctly.

Generate a new random interval only when the LED turns off and see what happens

Thanks! Got it to work, but now the led state is not toggling back and forth, just randomly?
I modified the location of Serial.print(interval); outside the loop, added a Serial.print(ledState), and added delay(interval);

I'm not sure how/where to rewrite in the code to generate a new random interval only when the LED turns off?
Can you show me the needed modifications?

Here's the current code:

// TrueRandomSeed.ino
//   This example sketch shows how to provide a truly random seed value to the built in 
//   library pseudo random number generator.  This ensures that your sketch will be
//   using a different sequence of random numbers every time it runs.  Unlike the 
//   usually suggested randomSeed(analogRead(0)) this method will provide a much more 
//   uniform and varied seed value.  For more information about the basic technique used
//   here to produce a random number or if you need more than one such number you can 
//   find a library, Entropy from the following web site along with documentation of how
//   the library has been tested to provide TRUE random numbers on a variety of AVR 
//   chips and arduino environments. 
//
//   https://sites.google.com/site/astudyofentropy/project-definition/
//           timer-jitter-entropy-sources/entropy-library
//
//   Copyright 2014 by Walter Anderson, wandrson01 at gmail dot com
//
 
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/atomic.h>
// The following addresses a problem in version 1.0.5 and earlier of the Arduino IDE 
// that prevents randomSeed from working properly.
//        https://github.com/arduino/Arduino/issues/575
#define randomSeed(s) srandom(s)
 
volatile uint32_t seed;  // These two variables can be reused in your program after the
volatile int8_t nrot;    // function CreateTrulyRandomSeed()executes in the setup() 
                         // function.
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated

void CreateTrulyRandomSeed()
{
  seed = 0;
  nrot = 32; // Must be at least 4, but more increased the uniformity of the produced 
             // seeds entropy.
  
  // The following five lines of code turn on the watch dog timer interrupt to create
  // the seed value
  cli();                                             
  MCUSR = 0;                                         
  _WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (1<<WDE); 
  _WD_CONTROL_REG = (1<<WDIE);                       
  sei();                                             
 
  while (nrot > 0);  // wait here until seed is created
 
  // The following five lines turn off the watch dog timer interrupt
  cli();                                             
  MCUSR = 0;                                         
  _WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (0<<WDE); 
  _WD_CONTROL_REG = (0<< WDIE);                      
  sei();                                             
}
 
ISR(WDT_vect)
{
  nrot--;
  seed = seed << 8;
  seed = seed ^ TCNT1L;
}
 
void setup()
{
  CreateTrulyRandomSeed();
  randomSeed(seed);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); 
  // The preceeding two function calls will take approximately 0.5 second to execute if 
  // nrot is set to 32 ... the rest of your setup code should FOLLOW from here.
}
 
void loop()
{
long interval = random(800,8000);

unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:

}
digitalWrite(ledPin, ledState);
Serial.println(interval);
Serial.println(ledState);
delay(interval);
}
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

Right there is where you decide ledState should be LOW. So I would say that if you want to calculate a new interval when ledState becomes LOW that would be the place.

You need to add { and } to that block if you're going to have multiple statements. You should be doing it anyway.

I tried to move the line after
ledState= LOW;
long interval = random(800,8000);
but I get an error: 'interval' was not declared in this scope

Also tried adding additional if after:
else
ledState = LOW;

if (ledState == LOW);{
long interval = random(800,8000);
}
Still only toggling the LED randomly, not back and forth? Needs to be coded in a different way?

Because the whole time ledState is low it will keep hitting that if.

What's wrong with doing this?

if (ledState == LOW){
ledState = HIGH;
}
else {
ledState = LOW;
long interval = random(800,8000);
}

That way it only happens when you make the change to set the ledState LOW.

I don't understand this code:

if (ledState == LOW){
ledState = HIGH;
}
else {
ledState = LOW;
long interval = random(800,8000);
}

interval gets set and then immediately goes out of scope (disappears).
It is close, but interval needs to be declared elsewhere. The code then looks like:

long interval;
.
.
.

if (ledState == LOW){
ledState = HIGH;
}
else {
ledState = LOW;
interval = random(800,8000);
}

THANKS! ;D Got it working after removing:
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:

Here's the current code:

// TrueRandomSeed.ino
//   This example sketch shows how to provide a truly random seed value to the built in 
//   library pseudo random number generator.  This ensures that your sketch will be
//   using a different sequence of random numbers every time it runs.  Unlike the 
//   usually suggested randomSeed(analogRead(0)) this method will provide a much more 
//   uniform and varied seed value.  For more information about the basic technique used
//   here to produce a random number or if you need more than one such number you can 
//   find a library, Entropy from the following web site along with documentation of how
//   the library has been tested to provide TRUE random numbers on a variety of AVR 
//   chips and arduino environments. 
//
//   https://sites.google.com/site/astudyofentropy/project-definition/
//           timer-jitter-entropy-sources/entropy-library
//
//   Copyright 2014 by Walter Anderson, wandrson01 at gmail dot com
//
 
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/atomic.h>
// The following addresses a problem in version 1.0.5 and earlier of the Arduino IDE 
// that prevents randomSeed from working properly.
//        https://github.com/arduino/Arduino/issues/575
#define randomSeed(s) srandom(s)
 
volatile uint32_t seed;  // These two variables can be reused in your program after the
volatile int8_t nrot;    // function CreateTrulyRandomSeed()executes in the setup() 
                         // function.
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long interval = random(800,8000);

void CreateTrulyRandomSeed()
{
  seed = 0;
  nrot = 32; // Must be at least 4, but more increased the uniformity of the produced 
             // seeds entropy.
  
  // The following five lines of code turn on the watch dog timer interrupt to create
  // the seed value
  cli();                                             
  MCUSR = 0;                                         
  _WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (1<<WDE); 
  _WD_CONTROL_REG = (1<<WDIE);                       
  sei();                                             
 
  while (nrot > 0);  // wait here until seed is created
 
  // The following five lines turn off the watch dog timer interrupt
  cli();                                             
  MCUSR = 0;                                         
  _WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (0<<WDE); 
  _WD_CONTROL_REG = (0<< WDIE);                      
  sei();                                             
}
 
ISR(WDT_vect)
{
  nrot--;
  seed = seed << 8;
  seed = seed ^ TCNT1L;
}
 
void setup()
{
  CreateTrulyRandomSeed();
  randomSeed(seed);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); 
  // The preceeding two function calls will take approximately 0.5 second to execute if 
  // nrot is set to 32 ... the rest of your setup code should FOLLOW from here.
}
 
void loop()
{

if (ledState == LOW){
ledState = HIGH;
}
else {
ledState = LOW;
interval = random(800,8000);
}
// set the LED with the ledState of the variable:

digitalWrite(ledPin, ledState);
Serial.println(interval);
Serial.println(ledState);
delay(interval);
}

Ooops. I should have seen that.

vaj4088:
I don't understand this code:

if (ledState == LOW){

ledState = HIGH;
}
else {
ledState = LOW;
long interval = random(800,8000);
}




interval gets set and then immediately goes out of scope (disappears).
It is close, but interval needs to be declared elsewhere. The code then looks like:



long interval;
.
.
.

if (ledState == LOW){
ledState = HIGH;
}
else {
ledState = LOW;
interval = random(800,8000);
}