How can i get a random value from a given array?


I have an array of values from 1-10. I need the arduino to choose only one random number from the given array. How can i do this?

I have looked through the random() command but it gives more than one value at a time. I want to be able to rename the numbers 1-10 and use then later on in the code to make decisions.

int angleArray[11] = {1,2,3,4,5,6,7,8,9,10,11}
const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0; 

void setup() {
  // put your setup code here, to run once:
a = angleArray[0]; 
b = angleArray[1];
c = angleArray[2];
d = angleArray[3];
e = angleArray[4];
f = angleArray[5];
g = angleArray[6];
h = angleArray[7];
i = angleArray[8];
j = angleArray[9];
k = angleArray[10];
pinMode(buttonPin, INPUT);

void loop() {
   buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {


What on earth are you up to with those single letter variables?

Random does not give more than one value at a time.

  1. How is the button wired? Normally you want to connect one side of an NO button (normally open, it, no connection until it is pressed) to the Arduino pin, the other side of it to ground, and have pinMode() on that pin set to INPUT_PULLUP - if the pin reads LOW, it is being pressed. If you do it the other way, with the button connecting the pin to 5v when pressed, you must include a pulldown resistor between the arduino pin connected to the button, and ground. Look up "floating pin" and one of the countless guides on using buttons with Arduino.

  2. I assume you put the call to random(), and the code to use that to choose a value, where that lone semicolon is. That is conceptually flawed - every pass through loop, it will check the value of the button, but you have nothing to prevent it from repeatedly running that bit of code while the button is held down. Since loop() will run very quickly (relative to human timescales), even a "brief" press on the button will result in that code executing many times, and selecting many values in succession. You need to record the previous state of the button, and only do things when it was previously not pressed, and is now pressed.

  3. You also need to debounce the button; this will be covered in guides to using buttons with Arduino.

  4. random() is deterministic; you will get the same pseudorandom sequence every time you run the sketch. You need to use randomSeed() and some source of entropy to ensure you get a different sequence each time. The simplest way to do this is to do randomSeed(analogRead(Some_Analog_Pin)); in setup(), where Some_Analog_Pin is an analog pin not connected to any external device - this will pick up ambient electromagnetic noise and get you a fairly random number.

I have an array of values from 1-10. I need the arduino to choose only one random number from the given array. How can i do this?

int angleArray[11] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
byte pickedIndex;

void setup() {
  pickedIndex = random(11);
  Serial.print(F("picked index is "));
  Serial.print(F(", value is "));
void loop() {}
picked index is 5, value is 6

With the given array values you would not need the array.

int   pickedValue = random(10) + 1;