Generating randoms values for arrays?

Hi, I need to make a sketch that has an array of 30 and I need to generate a value between 4-10 for each of them. I don’t know how to make it do so but here is the code I currently have

float Student[30] = {4, 4.25, 4.5, 4.75, 5, 5.25, 5.5, 5.75, 6, 6.25, 6.5, 6.75, 7, 7.25, 7.5, 7.75, 8, 8.25, 8.5, 8.75, 9, 9.25, 9.5, 9.75, 10};

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 29; i++)
  {
    int Grade = random(0,29);
    int t = Student[i];
    Student[i] = Student[numero];
    Student[numero] = t;
  }

  for (int i = 0; i < 29; i++)
  {
    Serial.print(i);
    Serial.print(": ");
    Serial.println(Student[i]);
  }
}

void loop() 
{

}

Thanks.

TakahuoneenSami:
Hi, I need to make a sketch that has an array of 30 and I need to generate a value between 4-10 for each of them. I don’t know how to make it do so but here is the code I currently have

float Student[30] = {4, 4.25, 4.5, 4.75, 5, 5.25, 5.5, 5.75, 6, 6.25, 6.5, 6.75, 7, 7.25, 7.5, 7.75, 8, 8.25, 8.5, 8.75, 9, 9.25, 9.5, 9.75, 10};

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 29; i++)
  {
    int Grade = random(0,29);
    int t = Student[i];
    Student[i] = Student[numero];
    Student[numero] = t;
  }

for (int i = 0; i < 29; i++)
  {
    Serial.print(i);
    Serial.print(": ");
    Serial.println(Student[i]);
  }
}

void loop()
{

}




Thanks.

What happened when you tried the example code for random?

Paul

Have a look at the TrueRandom library, it generates random number in various ways, and explains how to use it.

TrueRandom basic functions
The existing random functions of Arduino are replicated in TrueRandom.

TrueRandom.random()
Like the Arduino library and ANSI C, this generates a random number between 0 and the highest signed long integer 2,147,483,647.

TrueRandom.random(n)
This generates a random number between 0 and (n-1). So random(6) will generate numbers between 0 and 5.

TrueRandom.random(a,b)
This generates a random number between a and (b-1). So random(1,7) will generate numbers between 1 and 6.

TrueRandom advanced functions
TrueRandom.randomBit()
Generating true random numbers takes time, so it can be useful to only generate as many random bits as you need. randomBit() generates a 0 or a 1 with 50% probability. This is the core function from which the other TrueRandom libraries are built.

TrueRandom.randomByte()
Generates a random byte between 0 and 255. Equivalent to random(256).

TrueRandom.rand()
Like the ANSI C rand() command, this generates a random number between 0 and the highest signed integer 32767.

TrueRandom.memfill(address, length)
Fills a block of bytes with random numbers. (length) bytes are filled in total, starting at the given (address).

lesept:
Have a look at the TrueRandom library, it generates random number in various ways, and explains how to use it.

The quality is not very good...
http://www.endolith.com/wordpress/2013/06/19/truerandom-is-not-truly-random/

This is a far better choice...

(I believe Walter published the library on Github but I cannot find it.)

Interesting, thanks for the links!

    int Grade = random(0, 29);

Whatever means you use to generate the random number it is no good unless you use it in the program

By the way, the random statement above creates a number between 0 and 28, not 0 and 29

float value = random(400, 1001) / 100.0;