Problem with random()

Every time I run this sketch the DIV is 4.86 and the MP is 3.88 WHY??!!?! It has to be firstly randomized between 125 and 500 and then divided by 100 so I get value between 1.25 and 5.00

float MP; 
float DIV;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  MP = random(125, 501); 
  DIV = random(125, 501); 
  MP = MP / 100;
  DIV = DIV / 100;
  Serial.print("Dividor: ");
  Serial.println(DIV);
  Serial.print("Multiplier: ");
  Serial.println(MP);
}

void loop() {
  // put your main code here, to run repeatedly:

}

The random generator is pseudo random. If you only run it once you will always get the same number.

You can seed thr random number generator using randomSeed; see the reference.

I dont understand the randomSeed()

Explain to me how to fix this please

JMD1: I dont understand the randomSeed()

Lame excuse for being to lazy to read.

JMD1: I dont understand the randomSeed()

Explain to me how to fix this please

Microprocessors can't just come up with a random number. They are following strict instructions to calculate what looks like random numbers. To give give you another sequence every time you power up your arduino you have to set a new start value. A good way to do this is to read an unused analog input. This value will float and is affected by many variables, temperature, elctric field and more. Here is reference page you were asked to look at.

JMD1: I dont understand the randomSeed()

Explain to me how to fix this please

There is a complete example in the reference section; please explain what you do not understand?

randomSeed will not give a perfect initialisation. The The reliable but not very sexy way to seed random thread contains some example code to get a better randomSeed. Please read and understand the complete thread before using any of it.

The code in reply #6 also includes a way to make the seeding depending on your code. So different programs will result in different seeding.

reply #52 contains the latest version.

Guys I told you that I don't understand it and you started arguing? I can not understand why is this needed like what should be inside the brackets, why is this present and few other things. Please don't quote this.

The best answer was to Gabriel_swe, from his comment I already understood why this was. Thank you, Gabriel_swe.

Even using randomSeeed(analogRead(…)) will shield a prebuilt-in list of “random values”.

Instead, I read analogRead(A0)+…+analogRead(A7) in my Nano and take modulo (%), for example:

int GenerateRandom(int modulo)
{
int value = 0;
for (int a=0;a<8;a++)
{
value = value + (analogRead(a));
}
value = value % modulo;
return value;
}

it shields a random value betwteen 0 and modulo. modulo should be less than 1024 as analogRead() gives values between 0 and 1023.

Nothing is connected to the analog pins so their readings give random values as they are “floating”.

Regards,
Robert

I don't trust analog inputs for randomization. A large part of the time you will get zero or 1023. You're lucky if you get something in between.

Another source of randomness is user input. The number of milliseconds between switching on and the first button press is going to be reasonably random.

If you truly want cryptography-level randomness, use an Arduino variant such as one of the Teensy's which has a real entropy generator on board.

MorganS: I don't trust analog inputs for randomization. A large part of the time you will get zero or 1023. You're lucky if you get something in between.

That is why I enable the pull up resistor, disable it and wait a few seconds. It always drifts down towards zero at a random rate.

MorganS: I don't trust analog inputs for randomization. A large part of the time you will get zero or 1023. You're lucky if you get something in between.

As far as I can see, I'm the luckiest guy on Earth. I'm getting random values ranging from 0 to 10, which is what I need. Don't forget I'm taking %10 so even if the amplitude of the oscillating values is not high, it is high enough to produce those values. I'm running the sketch right now.