1
#include "mbed.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
InterruptIn Button(USER_BUTTON); 

random_led = rand[led1, led2, led3]
int main()
{
    while(1)
    {   
        if (Button==1) {
            led1 = 1;
            led2 = 1;
            led3 = 1;
        } 
        else{
            led1 = 0;
            led2 = 0;
            led3 = 0;
          wait(3);
          random_led = 1;
    }
}

I understand that my code does not work however I cannot get my head around how to select a random LED, I have tried to use a random string method but that ran to a dead end.

HelpingHand
  • 1,294
  • 11
  • 27
Dildan
  • 25
  • 4
  • 2
    You could get the system clock tick to be your random number generator and use the modulo operator (%) to select one of the LEDs. – campescassiano May 13 '20 at 23:32
  • 2
    @campescassiano has a very popular suggestion. If you can sprinkle in time after first "user input" - which tends to be very random, you have the random system used on many older game consoles. – Michael Dorgan May 13 '20 at 23:47
  • 1
    Another possibility, if you have a real-time clock or analog-to-digital inputs available, reading them out can give you some entropy to seed a [pseudo-random number generator](https://en.wikipedia.org/wiki/Pseudorandom_number_generator). – G. Sliepen May 14 '20 at 17:52
  • Another common approach is to use the jitter between clocks. Have a look at this as an example: https://stackoverflow.com/questions/14796398/how-generate-real-random-number-using-stm32-mcu – chumbaloo May 15 '20 at 13:13

0 Answers0