# Help random number

Hi I want to do a random list I need that 13 numbers have a random behaviour each time I turn on my Arduino - I'm a visual artist and I don't have a lot of experience programming I was going to program it with something like this
randNumber1 = random(13);
randNumber2 = random(13);
randNumber3 = random(13);
randNumber4 = random(13);
randNumber5 = random(13);
randNumber6 = random(13);
randNumber7 = random(13);
randNumber8 = random(13);
randNumber9 = random(13);
randNumber10 = random(13);
randNumber11 = random(13);
randNumber12 = random(13);
randNumber13 = random(13);

if (randNumber1 = randNumber2)
{
randNumber2 = random(13);
}

if (randNumber1 = randNumber2)
{
randNumber2 = random(13);
}

if (randNumber1 = randNumber3)
{
randNumber3 = random(13);
}

if (randNumber1 = randNumber4)
{
randNumber4 = random(13);
}

if (randNumber1 = randNumber5)
{
randNumber5 = random(13);
}
if (randNumber1 = randNumber6)
{
randNumber6 = random(13);
}
if (randNumber1 = randNumber7)
{
randNumber7 = random(13);
}
if (randNumber1 = randNumber8)
{
randNumber8 = random(13);
}
if (randNumber1 = randNumber9)
{
randNumber9 = random(13);
}
if (randNumber1 = randNumber10)
{
randNumber10 = random(13);
}
if (randNumber1 = randNumber11)
{
randNumber11 = random(13);
}

if (randNumber1 = randNumber12)
{
randNumber12 = random(13);
}
if (randNumber1 = randNumber13)
{
randNumber13 = random(13);
}

And so it with each randNumber but I'm sure there is a easier more productive way to do it , can anyone help me

Thanks

I am guessing you don’t want duplicated numbers?

``````#define TOTAL_NUMBERS 13
int n[TOTAL_NUMBERS];

void setup()
{

// Fill initial values
for(int i=0;i<TOTAL_NUMBERS;i++)
n[i]=i+1;

// Randomize (swaping)
int p1, p2, v;
for(int i=0;i<TOTAL_NUMBERS;i++)
{
p1 = random(TOTAL_NUMBERS);
p2 = random(TOTAL_NUMBERS);

v = n[p1];
n[p1] = n[p2];
n[p2] = v;
}
}

void loop()
{
// Use random numbers here n[0-TOTAL_NUMBERS]
}
``````

You are a genius tks tks tks

``````const int totalNumbers = 13;
int randnums[totalNumbers];

void setup() {
int availablenumbers[totalNumbers];
for (int i=0; i< totalNumbers; i++) {
availablenumbers[i] = i+1;
for (int i=totalNumbers; i>=0; i++) {
int n = random(i);
randnums[i] = availablenumbers[n];
for (int j = n; j < i-1; j++) {
availablenumbers[j] = availablenumbers[j+1];
}
}
}
``````

here’s another idea that may or may not work.

Wanderson has a nice random number generator that seems to work well. I had occasion to run some test's for him and it worked well on 2 Uno's a Mega and 2 Pro Mini's.

Doc

eried’s approach optimized,

no need to swap two random elements as you can swap over loop iterations. which is faster (however as it is only executed in setup it is no big deal)

``````const int totalNumbers = 13;
int randnums[totalNumbers];

void setup()
{
for (int i=0; i< totalNumbers; i++) randnums[i] = i+1;

int freeslot = 0;
for (int i=0; i< totalNumbers; i++)
{
int n = random(totalNumbers);
randnums[freeslot] = randnums[n];
freeslot = n;
}
randnums[freeslot] = 1;
}
``````

NOte: the Arduino will allways give the same sequences with the random function - http://arduino.cc/en/Reference/random -

`````` if (randNumber1 = randNumber6)
``````

I'm guessing that was unintentional.