Filling an integer array with unique random values

As the title suggests, I want to fill an integer array (int a[8]) with unique values from 0-7 during each iteration of loop fuction in Arduino.I read around the net that the most sane idea I found involved filling array with these values(0-7)sequentially and then swapping these values a random number of times.How do I code this mechanism in arduino?

Have you looked at arduino.cc and searched for random?

I did, and, they have a few functions devoted to random numbers. randomSeed() will let you select the seed number that is used for random number generation.

http://arduino.cc/en/Reference/RandomSeed

One trick you could try if

One trick you could try if you have a free analog pin.

Let the pin float (don’t connect to anything. A floating pin will have noise that randomly fluctuates from 0-1023)

Read the analog pin value and divide by 146.

This should give you a TRUE random number between 0 and 7.

Loop this routine for each element in your array.

 

Hi birdmun,as I see it…

I thought about using randomseed but rejected the idea after having already tested it and then reading this on help page- "This sequence, while very long, and random, is always the same."Since I have a domain of 8 numbers I found the result unsatisfactory…

Moreover I don’t have an analog pin available as all are in use.The catch here is that without using an analog pin with random seed gives a sequence which pretty much mimics random() function(As I found out after several iterations).

So,a reasonable way forward is I guess to write a custom shuffle() function which can address the issue more better(randomly in this case)I think…

Hey JerZ,

There are two reasons of concern here:

1) I don’t have any analog pins available.

2) Even if I employ the above mechanism,146 is a large range and I said fill each number “Uniquely”.More often than not ,like if arduino selects 988 and 1002 we get the same number 6.Thus this contradicts the uniqueness clause that I need.

So

instead of using an analog pin, use the counter. It starts when the chip fires up and will likely be different most every iteration through and batch of loops you care to go through.

what are you going to use the array for?

?

Randomness my friend…

Has countless uses.You could light up leds in random manner,connect output of it to piezo and listen random music.You could even script an arduino luck based game with it.Sky’s the limit!

Sorry, I thought you had a real need.

You are just playing, I see. 

Generating randoms is a sticky widget sometimes. Have fun!

Breakthrough !

I just scripted this code and it works(I believe).Posting it here for anyone else who wants “Variations” in their code…

int enemy_col[8] = {0,1,2,3,4,5,6,7};
int x,y,temp;
void setup() {
  Serial.begin(9600);
  display();
}//setup

void display(){//display array
  for(x = 0;x < 8;x++){
    Serial.print(enemy_col[x]);
    Serial.print(" ");
  }
  Serial.println();
}

void swap(){
  int swap_count = random(4,8);//No of time to swap numbers in array
  //Serial.print("Swap Count = ");
  //Serial.println(swap_count);
  for(int i = 0;i <= swap_count;i++){
     x = random(0,8);//Choosing 1st number to swap randomly
     //Serial.print("x = ");
     //Serial.println(x);
     do{
       y = random (0,8);//Choosing 2nd number to swap randomly ,making sure its diffrent from 1st number
     }while (x==y);
     //Serial.print("y = ");
     //Serial.println(y);
     //swap numbers in array
     temp = enemy_col[x];
     enemy_col[x] = enemy_col[y];
     enemy_col[y] = temp;
    }//for
}
void loop() {
  for(int k = 0;k < 30;k++){
    swap();
    display();
  }
}

Check this out on your Serial monitor guys and tell me if its as random as It can get …

P.S. Why is the loop not stoping in serial monitor?

 

Try adding

randomSeed(millis()); before the for loop and be for the do … while loop in your swap function.

Thanks bird,

I think we’ve achieved the height of randomness:0 1 2 3 4 5 6 7
0 6 1 3 4 5 2 7
0 7 4 6 3 1 2 5
4 5 7 6 3 1 2 0
4 5 0 1 6 3 7 2
4 5 0 7 2 1 3 6
7 4 2 0 5 1 3 6
1 2 7 0 5 4 6 3
1 0 7 3 4 2 6 5
1 4 5 7 0 2 6 3
3 4 5 7 1 2 6 0
3 4 1 7 2 6 5 0
2 1 3 4 6 7 5 0
5 2 3 4 6 7 0 1
5 2 3 7 4 0 6 1
5 6 2 7 3 0 4 1
6 4 2 7 0 5 3 1 and the list goes on and on…

But my new problem, that I mentioned in above code is that the serial monitor goes crazy and is continously printing values while according to the for loop in the loop function It should stop after 30.Why is that happening?

Just something to "no pin

Just something to “no pin available”. You could set an analog pin in the setup() to create a random number and then re-set it to the feature you need to use it in your application.

 

**I would suggest you add another print statement to help with **

debugging your code. Insert a Serial.print(k); in your for loop. you can see what the value is while it is running.

.

.

Thanks bird! Finally Code Debugged.

The for loop inside loop function was running indefinitely and k was being re initialized to 0 after it reached 29.I assigned k=0 in setup and Replacing “for” with while(k<29) and incrementing k in while loop solved the problem.And now ladies(I doubt if there are any) and gentlemen in LMR, we have achieved complete chaos without using random or randomseed but a custom function.Bravo!