Need some help

Hello everyone, I just took a year long hiatus from Arduino and I've forgotten everything I used to know. I'm trying to make a button turn on an LED for a random ammount of time but when I press the button, it flickes twice and turns off. The timing is the same every time and it only works propperly if i hold the button for a little while and then release it. Heres the code:

int pushButton = 2;
int LED = 13;
int buttonState = 7;
long randOn = 0;
long randOff = 0;
void setup() {
  pinMode(pushButton, INPUT);
  pinMode (LED, OUTPUT);
void loop() {
  int buttonState = digitalRead(pushButton);
  if (buttonState==HIGH)
   randOn = random (100, 1200);
   randOff = random (200, 900);
   digitalWrite(LED, HIGH);
   digitalWrite(LED, LOW);


it only works propperly if i hold the button for a little while and then release it.

I bet that is indeed the case. You should go study the "Blink without Delay" example. During those delay statements, which look like they can be a second or more long, you're not reading the button or doing anything else. So you certainly won't notice any button pushes until you come out of all those delays and get back to the line that reads the button.

Hi Steve!
You don't have randomSeed().
I don't understand why you need to do:

   digitalWrite(LED, LOW);

Or better, I don't understand why you need to wait after turn off the LED. When you turn off the LED it will be off until the next time that you push the button, so, why wait?

The same for the:

   digitalWrite(LED, HIGH);

delay before you turn on the LED. I don't see the need of this.

EDIT: I forgot to point this link. It talk about ramdom and randomSeed.

Delta_G, I don't think that in this case, that can make a great difference.

He should be using the Blink Without Delay method and he should also have a flag that lets the code know when the LED is on and off. This way it cuts down the processing time.