Generating a random number

I am creating a program in processing where one number is randomly chosen and is printed to serial. The code i am using for some reason always gives me a reading of 748. I know that random() will create a random number but it dosn’t stop creating them.

int Number = int(random(1024));

Hi Nic,

randomSeed should be done once only, during the initialization of the device. Move that out of your loop and you will see “randomised” numbers.

I put that in quotation as the sequence of numbers will always be the same when seeding with a constant. Every time you reset the device, the sequence will begin from 748. There are many ways to seed with a random value, most popular for MCUs is to save the last generated value in EEPROM and use that as the seed upon reset. This will emulate “random” - albeit not something banks would use for encoding secure money transfers.

Bear in mind that ATmega328 EEPROM is only good for about 100,000 write cycles. So if you are generating LOTS of random numbers, then perhaps also use a counter and take every Nth value into EEPROM (eg, every 100th or 100,000th, whatever works for you).