adding randomness

Hello. I found a code online for controlling 6 LEDs. There's a section for lighting them in a loop, and it does the LEDs in the same order every time. I want to make it choose them randomly. I think I need something like random(1,6), but I'm not sure that's correct or how to add it. I'm completely new to this. Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
  // EXCITED =========================================
    // + Flash each LED, one at a time, six times in a row
    case EXCITED:
      for(int j=0; j<6; j++) {
        for(int i=1; i<=6; i++) {
          turnOn(i);
          delay(50);
          turnOff(i);
          delay(50);
        }
      }
srand(time(0));
turnOn ( rand()/7 );
closed account (1CfG1hU5)
some ways to initialize random number generator

 
srand(time(0);


 
srand(time(NULL);  // same as 0 


 
srand((unsigned) time(NULL));  // a way seen in dos compiler 


1
2
time_t t;
srand((unsigned) time(&t));


 
randomize();  // dos compiler function.  define in header is srand((unsigned) time(NULL)); 

Last edited on
The use of srand/rand is deprecated.

For current C++, see http://www.cplusplus.com/reference/random/
Last edited on
The use of srand/rand is deprecated.

isn't it too early to call that?
i know <random> library is introduced in C++11, which provides extensive random number support. but for simple cases srand/rand still relevant. many best selling(recent)books support that:
C++ How to program - Deitel 8th ed. (2011) p207
starting out C++ - Tonny Gaddis 8th ed.(2014) p127
C++ programming - DS Mallic 6th ed.(2012) p275
Absolute C++ - Walter Savitch 5th ed.(2012) p110
Topic archived. No new replies allowed.