Pages: [1]   Go Down
Author Topic: New random number loop  (Read 445 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Im trying to create an array of 5 different numbers between 1 and 20.

However i need to make sure all the numbers are unique.  Obviously if I request a new random number each time then theres a good chance there will be duplicates.  How do I code this?

At the moment I have:

Code:
for(int x = 0; x < 5; x++) {
  int i = random(1,21);
  myArray[x] = i;
}


Thanks smiley
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13836
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


one solution is put all numbers in an array, scramble them and take the first 5.

Code:
  int numbers[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13 };
  int count = 13;


void setup()
{
  Serial.begin(9600);
 
  scrambleArray(numbers, count);
  for (int i=0; i<count; i++)
  {
    Serial.println(numbers[i]);
  }
  Serial.println("----");
}

void loop()
{}

void scrambleArray(int * array, int size)
{
  int last = 0;
  int temp = array[last];
  for (int i=0; i<size; i++)
  {
    int index = random(size);
    array[last] = array[index];
    last = index;
  }
  array[last] = temp;
}

Another solution is to remember the values you got "thrown", if one is already in the list rethrow the dice ...

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd be a bit worried that you put 14 numbers in that array and then sorted 13 of them.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

However i need to make sure all the numbers are unique.  Obviously if I request a new random number each time then theres a good chance there will be duplicates.  How do I code this?

One method is for each element in the array, keep generating numbers until you find one that is not already present in the array.

I'm not going to code it for you, but you would need:
  • A for loop to process each element in the array.
  • A while loop inside it to process the current element repeatedly until a unique random number had been found
  • A for loop inside that to compare the currently proposed random number against all the preceding elements in the array
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13836
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'd be a bit worried that you put 14 numbers in that array and then sorted 13 of them.
At least someone notices smiley-wink

It shows a degree of freedom the algorithm has, you can randomize only first part of the array smiley-wink


Drawback of algorithms that need to check if there a number is already chosen like the one I proposed myself (last line of prev post) that they can take a large amount of time, especially when the list of numbers to choose from and the numbers needed are in the same order of magnitude.

There is also the algorithm (Dijkstra/Knuth?) that is linear with the amount of numbers. This algorithm generates one number per iteration but it needs an array with all numbers to be chosen from. It works as follows:

take an array of 20 elements.
take one element x=random(20)
remove element x from the array by moving the last element of the array to x
make the array 1 element smaller; //ignore last element
take one element x=random(19)
remove element x from the array by moving the last element of the array to x
make the array 1 element smaller; //ignore last element
etc..

(snippet)
Code:
for (int i = 0; i < 5; i++)
{
  int x = random(20-i);
  Serial.println(array[x]);
  array[x] = array[20-i];
}

Drawback of this algorithm is that it needs the array of numbers to choose from.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: