Help Creating Randomness

Hello,

I am a beginner using the Arduino software and have an Arduino Nano. Just wondering if there was a way to make it so that a random LED will turn on for a second, for example:

after 1 second Either pin 2, 3, 4, or 5 will turn high for a second, and the go back to low

Thanks,
Harry

here you go! Peruse this: random() - Arduino Reference

// Code

int pins[] = {2,3,4,5};

int pinCount = 4;

void setup() {
  delay(1000); // 1000 milliseconds = one second


  int pin = random(2,5)
  
  for (int i = 0; i < pinCount; i++) {
    pinMode(pins[i], OUTPUT);
  }

  digitalWrite(pin,HIGH);

  delay(1000);

  digitalWrite(pin, LOW);
}

This should work, tell me if it doesn't.

Note that you can't get a true-random number from a computer algorithm. The issue becomes making it "random enough" that it's not predictable, and that's usually possible.

If you don't use any "ticks" you'll get the same sequence of random numbers every time you run the program.

If you seed the random number generator with analogRead() you can get some variation.

If you have a "real" unpredictable analog input you can get good randomness. I have an application with an audio input so that works very well as a random seed.

I did an experiment once, and with the analog input floating (nothing connected) the readings were jumping around by 3 or 4 counts. I connected a coil of wire to an analog input to pick-up noise (nothing connected to the other end of the wire) and the numbers were jumping around about 10 counts. When I touched the wire (using by body as an "antenna" to pick-up noise) I was getting the full 0-1023 range.

If there is human input, like pressing a button, you can make a loop at the beginning of the program, read millis() when a button is pressed, and use that as a seed.

Or, if you can push a button every time you want a random number, you can read random numbers in a continuously loop and "grab" the latest number when the button is pushed. In that case it's not necessary to seed.

I've read that's how (modern electronic) slot machines work... The random number generator runs full-time in the background and it's locked-in when you pull the handle or push a button. (I've also read of more exotic methods with quantum physics and noise generators, or something...)

...Or, if your program is gong to run continuously in a loop, changing the LED randomly every second, your brain won't be able to remember much of the sequence. You'll probably just recognize the 1st part ever time you re-start the program and then it will look random. :wink:

is nonexistent. Everything in the universe has already happened, just in the future. There is nothing random. Randomness is just a human's limited understanding of a pattern too complex for him or her to understand. I repeat myself: everything has already happened, following a specific, orderly, prepared pattern, just most of it is in the infinite future, both in this world and the next. (Just some brain food :wink: )

Randomness is everything. Chaos is natural order. We seek to create human order, but none exists, then try to re-invent "randomness" when it is everywhere. Computers do not do random because humans can't make random. Mow your lawn (order) and wait a month for chaos to reclaim its property. Build a plateau in Europe and in no time at all random Alpeness is on your doorstep. An electron from my body passed through your morning scone and by evening may or may not pass through the Great Wall of China, or join with elements not yet created by the True One, random chaosness. "It is it."

No. There is no chaos or random. Chaos (Greek khaos) means nothingness. If chaos claims something, that means it is nothing. If you mow your lawn (order) and wait a month, the grass is not khaotic. It knows exactly what it is going to do, just not yet. Everything has already happened, just not yet. Everything (not including supernatural events) is predictable, if even on a microbic or sub-subatomic level. Three things are not predictable:

1: Right and Wrong: Humans have souls, unlike animals. We know right and wrong, animals don't. This means that an animal would kill to eat, humans might feel sympathy for the animal and go hungry. This is unpredictable.

2: Supernatural events

3: THERE IS NO THREE

Anything else is predictable. Little example: a dog decides not to fetch the ball. What could the dog be thinking? "SQUIRREL", "Need to go potty", or "Ball Stinky" are three options. All of them are predictable. For the first, there is a squirrel that has been taunting the dog for three weeks straight and the dog needs revenge; for the second, the dog's bladder may or may not be full, and for the third, the ball might have been left out in the rain, in the compost, maybe. Each of those reasons has at least one reason behind them. Almost nothing is unpredictable.

P.S. This is no longer a project guidance topic. @UKHeliBob, could you please put everything after the second post into a new topic, preferably titled "Discussion about Randomness" in the Bar Sport category? Thank you!

"Space"

Made up. Post one between < CODE > tags.

Mental disorder/chaos.

No. A few define "right and wrong"

What have I in my pocket?

@xfpd and @Delta_G you obviously aren't as highly educated as me :triumph: As @Delta_G said:

Infinity does not exist.

ok, sorry

or so you think... :thinking: