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.

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. May 13, 2020 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. May 13, 2020 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.
    – G. Sliepen
    May 14, 2020 at 17:52
  • Another common approach is to use the jitter between clocks. Have a look at this as an example: stackoverflow.com/questions/14796398/…
    – chumbaloo
    May 15, 2020 at 13:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.