I have a project, in which i need to choose between 4 numbers.I used random(2,5) because those were the values I need.However, i need to make sure each return is different.Ex. I can't have the same number repeat. How do I do this?

keep track of the most recent random number. Then before allowing a new random number, compare it to the variable. If it's different, do what you need to do with it. If it's the same, keep generating new ones until it's different.

You want a shuffle algorithm. Put the four numbers into an array. Pick one at random. Remove that. Pick one of the remaining three at random. Remove that. Pick one of the remaining two at random. Then you have the last one.

Please post technical questions on the forum, not by personal message. Thanks!

Just a few coding observations that can make your code easier to read and easier to understand:

You have 2 delays, 3 sec at the beginning of your loop and 1 sec at the bottom of the loop, that can be combined into a single delay.

// Comments help to explain what you are doing. I had to read through the code to determine the following:

It appears that you have a loop cycle time of 6.8 seconds when you add all of the delays. Within this time you would like to randomly flash some LEDs for 0.7 seconds.

From your code, it appears that you can flash an LED multiple times in the cycle--just not twice in a row, and that within a cycle it is ok if an LED doesn't flash at all. If not, then Nick's shuffle approach is the way to go.

That said... This was probably just supposed to be a fun activity and now that you've gotten it to work, you can move on to other things! FUN, FUN, FUN. If you're looking for something to tweak (and something to learn), maybe use potentiometers to vary the loop delay and on/off delays?

There are 10 types of people in the world, those that understand binary, and those that don't.

Just a few coding observations that can make your code easier to read and easier to understand:

You have 2 delays, 3 sec at the beginning of your loop and 1 sec at the bottom of the loop, that can be combined into a single delay.

// Comments help to explain what you are doing. I had to read through the code to determine the following:

It appears that you have a loop cycle time of 6.8 seconds when you add all of the delays. Within this time you would like to randomly flash some LEDs for 0.7 seconds.

From your code, it appears that you can flash an LED multiple times in the cycle--just not twice in a row, and that within a cycle it is ok if an LED doesn't flash at all. If not, then Nick's shuffle approach is the way to go.

That said... This was probably just supposed to be a fun activity and now that you've gotten it to work, you can move on to other things! FUN, FUN, FUN. If you're looking for something to tweak (and something to learn), maybe use potentiometers to vary the loop delay and on/off delays?

Patduino, thanks for the tips! What the code does is flash each LED once, making sure that each is flashed only once.

There seems to be a few other discussions happening here... I'll try to address your comment:

Quote

What the code does is flash each LED once, making sure that each is flashed only once.

If you are happy with your code, then ignore me... But I believe your software won't do what you wanted. (1) the way you check for repeated random numbers wont guarantee that an LED wont repeat (walk through your code with the sequence: 2 3 2 3) and, (2) the whole thing is in a loop, so this will all happen more than once.

Just trying to help. I hope I'm not being too picky!

There are 10 types of people in the world, those that understand binary, and those that don't.

If you are happy with your code, then ignore me... But I believe your software won't do what you wanted. (1) the way you check for repeated random numbers wont guarantee that an LED wont repeat (walk through your code with the sequence: 2 3 2 3) and, (2) the whole thing is in a loop, so this will all happen more than once.

Just trying to help. I hope I'm not being too picky!

Thanks, Pat, You're not being picky at all! I see what you mean by a repeat, now... So I guess the only way to make it work would either be an array, or assign val 1-4, and then check each previous one.About the loop, that's not much of an issue as much as getting the LEDs to flash in a random sequence.Thanks for your help!MT