random

Hello, random can be used before setup ()?

#include <Arduino.h>
#include <oblast.h>


Oblast oblast;
Oblast oblast2(3);

void setup() {
  Serial.begin(9600);
  oblast2.vystup();
  randomSeed(analogRead(0));
}
void loop() {
  oblast.vystup();
  delay(50);
}

in a declaration he would like to use a randomly generated number

header

#ifndef __OBLAST_H__
#define __OBLAST_H__

class Oblast
{
    private:
        int pocetSten = 0;
        long nahodneCislo;
    public:
        Oblast(int);
        Oblast();
        void vystup();
        int nahoda();
};
#endif

body

#include <Arduino.h>
#include <oblast.h>

Oblast::Oblast(int pocet_sten) {
    pocetSten = pocet_sten;
}

Oblast::Oblast() {
    pocetSten = this->nahoda();
}

void Oblast::vystup(){
    Serial.print("Pocet sten je: ");
    Serial.println(pocetSten);
}

int Oblast::nahoda() {
    nahodneCislo = random(6, 200);
    return nahodneCislo;
}

You can do that, but since you call random() before seeding the random function, pocetSten will have the exact same value every time the program runs. Using analogRead() in the constructor to seed the random function may be problematic. In general, it's not a good idea to try to use the hardware in a constructor.

Instead of trying to use the hardware in the constructor, I recommend considering the common approach of creating a begin function, that you call from setup(). That way, you know the hardware has been correctly initialized before you try to use it.

The random number data does not necessarily have to be popped from the analog pin. I do not require any accuracy. It keeps me any number in the range. I tried to use both the input for randomSeed and the integer value even so it still gave me the same output

Passing a fixed value to randomSeed() will still result in random() providing the same pseudo-random number sequence every time the program is run. randomSeed() just sets your position in the sequence. If you always start from the same position, then you always get the same sequence.

I can't say whether that is a problem for your application. For example, if you were making a machine for gambling, it would be an issue because the player would know they can just unplug the machine and then they will get the same results every time.

Although there are other ways of getting a random input for randomSeed() (this being my preference), I think they will all involve use of the hardware.

I once did a command line game. Where two characters, a player and a computer, fought each other. Now I'm trying to make this game for Arduino. When, for example, the lives of a post will be represented by an LED. And this is the only thing I'm stuck with. Because the original game was able to create arenas of random shapes that gave different classes of characters the advantage of magic, etc.

So it didn't help, I'm going to lose it by randomly generating areas

I think the random arenas is a nice feature. I wouldn't give up on it. Did you try my begin() function suggestion?

He did not try, but I confess without torture that I overlooked the begin method somehow. I'm going to try