Problem with printing out a random number

I'm making an Arduino print out 1 random number on an LCD screen. I am so far unsuccessful. This is my code so far, and I am not smort enough to figure out what's wrong with it. Can anybody help?

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <EEPROM.h>

void reseedRandom( void )
{
static const uint32_t HappyPrime = 937;
union
{
uint32_t i;
uint8_t b[4];
}
raw;
int8_t i;
unsigned int seed;

for ( i=0; i < sizeof(raw.b); ++i )
{
raw.b[i] = EEPROM.read( i );
}

do
{
raw.i += HappyPrime;
seed = raw.i & 0x7FFFFFFF;
}
while ( (seed < 1) || (seed > 2147483646) );

randomSeed( seed );

for ( i=0; i < sizeof(raw.b); ++i )
{
EEPROM.write( i, raw.b[i] );
}

reseedRandom();
}
// gives the display a name lcd that can be used
// 16, 2 = 16 spaces and 2 lines
// 0x27 = default address of the display
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
// needs to be at the start of the screen commands
// try lcd.init()
lcd.begin();
// turns the backlight of the LCD off
lcd.backlight();
// clears the contents of the screen
lcd.clear();
// set the position that writing will start
// 4 = 4 spaces along
// 0 = line 1, 1 = line 2
lcd.setCursor(4,0);

}
void loop(){
lcd.print(reseedRandom());
delay(750); // Delay a little

}

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

While you're doing that, you could also research the maximum integer value that can be represented by an unsigned int (your 'seed' variable)...

You are not using the C++ function RAND() so I assume you have a purpose for the convoluted approach (perhaps you feel RAND() is not random enough.

Also have you previously proven the LCD and EEPROM code works? If not I would start there.

You need an lcd.print(); to put something on the display.

You have 'reseedRandom()' calling itself so it will never exit. Eventually it will run out of memory and crash.

If you want a random number you need a source of randomness. Most examples read an analog input that is floating. Not a great source but better than a crashing program. Get rid of your 'reseedRandom()' function. Call 'randomSeed(analogRead(A0)); in setup() and lcd.print(random()); in loop().

2 Likes

I probably shouldn't give code advice :slight_smile:

A thought though, do you think a random few digits of the current milli() provide a reasonable random number?

It's an old buggy version of the reliable but not very sexy way to seed random.

Hi, @emperor_platypus
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Why have you got references to primes and eeproms.

Can I suggest you just get the random number generator working through the IDE serial monitor before thinking about LCD and EEPROMS.

What is your ultimate aim?

This link shows you how to generate a random number and seed it.
There is even sample code you can use.

It looks like you have generated a lump of code and are now trying to debug it.
Develop your code in stages, getting each stage working before going to the next.

Tom.... :smiley: :+1: :coffee: :australia:

See post #7.

Thank, you... I apologise, I'm new to this.

hehe yeah lol I am very bad at coding but Comp Science commands it so I just look around forums

Thank you :slight_smile:

Seems like an odd course of study to pursue given your ability (or lack there of).

You are also attempting to print the return value of a function that is declared as void, so has no return value.

As for the actual reseedRandom function, using a union for type punning may not be supported by all compilers.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.