Saving 1000 Random numbers in an array and print it at once

Hi, I am trying to generate an array of 1000 values with Random() function. I want to save all the 1000 values into the array first and then print it once full. I am getting a result with a Serial.println in the for loop, but i want the result to come in one go as i want the sampling time to be as small as possible. Any possible help would be appreciated.

float a[1000] ;
int i;
void setup()
{
  Serial.begin(9600);
 }
void loop() {

 for ( i = 0; i < 1000 ; i++) {

  a[i] = random(8,15);
  }

  for (i = 0; i < 1000; i++); {
    Serial.print (a[i]);
  }
  Serial.println(" End");
}

int count = 0;
const maxCount = 1000;
void loop() {

  a[count] = random(8,15);
count++;
if ( Count >= maxCount )
{
 //do the print thingy
for (i = 0; i < maxCount; i++); {
    Serial.print (a[i]);
  }
count =0; // put count back to 0
  }
}

..

float a[1000] ;

Most Arduino (Uno, Nano 3, Pro Mini/Micro etc) do not have enough ram memory for that. It would require 4,000 bytes.

  a[i] = random(8,15);

The random() function will only produce integers, not fractions/decimal places.

What do you mean by "sampling time'? What is being sampled here?

The speed of your code here is limited by the baud rate, 9600. The time taken to generate the random numbers is tiny by comparison, so saving them to an array won't make any noticeable difference.

Hi Paul, Thanks for the reply. I dont really need the input with decimals. I used float just so that i have more memory than with int (Guess that was a terrible move). My ultimate aim is to input the values into the arduino through a port and generate a graph with the values recieved through it. What i am currently doing with the program is a sample, simulating the values recieved through the port using a random() function.
By sampling time i mean the graph which i need to generate has a time period of 1.5ms. So i would need the sampling of the board atleast half of that (which i am no way near)
Note: i use an Expressif ESP32 board.

Thanks Idahowalker. When i use your code, i am getting a lot of 1s and 0s. I will attach the code below with my edits.

int count = 0;
int maxCount = 100;
int i;
int a[100];

void setup() {
  Serial.begin(9600);
}

void loop() {

  a[count] = random(8, 15);
  count++;
  if ( count >= maxCount )
  {
    //do the print thingy
    for (i = 0; i < maxCount; i++); {
      Serial.print (a[i]);
    }
    count = 0; // put count back to 0
  }
}

By declaring int i as a global if, after the for loop has ran once and max count is equal to 100 and i is not set back to 0 before the next for loop is ran, the for loop thingy will not work as expected because i still has a 100 in it.

Has anyone pointed out the extra ';' that is preventing your loop from doing anything?

what do you mean you will HAVE more memory?? The memory is fixed, float takes 4 bytes, int takes 2 bytes, so using float instead of int when you don’t really need it you DOUBLE your memory usage for no reason

It will make sure array is accessed out of bounds, so worse than doing nothing

const unsigned SampleCount = 500;

int Array[SampleCount] ;

void setup()
{
  Serial.begin(115200);
  delay(200);

  unsigned long before = micros();
  for (unsigned i = 0; i < SampleCount ; i++)
    Array[i] = random(8, 15);
  unsigned long after = micros();

  for (unsigned i = 0; i < SampleCount ; i++)
    Serial.print(Array[i]);

  Serial.println();
  Serial.println(after - before);
}

void loop() {}

Can't fit 1000 floats in an UNO. Not even 1000 ints. Works fine with 500 ints.

Takes about 91 microseconds per random number.

@andsur is using esp32, so int will be 4 bytes. Not sure how many bytes float will be on esp32. But whatever, esp has plenty of ram for 1000 array indexes.

I must have looked at your post and thought they were using uno

If all your numbers will be less than 16, a byte will hold 2, one in low nybble, one in high nybble.

OMG! Thank you so much!! These kind of mistakes makes me wanna kill myself :sweat_smile: