1

I have a fixed array of sized nine and I am trying to reorder it randomly without duplication.

this is the following code:

class numbers{
    int randomIndexCount;
    public:
        void randomArray( int numArray[],int size){
            randomIndexCount = 0;               
            for (int i = 0; i < size; i++)
            {   
                int RandomIndex = rand() % size;
                randomIndexCount++;
                numArray[i] = numArray[RandomIndex];
                cout << numArray[i] <<endl;                 
            }           
    }

    int main(){
        srand(time(0));
        int numArray[9]= {1,2,3,4,5,6,0,0,0};
        numbers n;
        n.randomArray(numArray,9);
        return 0;
    }

So far I was able to reorder the array randomly with the given elements however I am unsure how to get rid of duplication. the output should be {1,2,3,4,5,6,0,0,0} but in a random order. I am unable to use the shuffle function and can only use rand. I am not sure how to remove duplicate entries

this is what I had in mind 1) with the given index check if that value already exist and if it does then skip this line " numArray[i] = numArray[RandomIndex];". however this approach would not be efficient as im sure this would be too time consuming. is there a way to remove duplicate values so my output is something like:

{0,1,0,6,2,0,5,3,4}
10
  • 3
    Use a Fisher-Yates shuffle algo: geeksforgeeks.org/… Commented Mar 5, 2020 at 10:55
  • 1
    Removed duplicated manually and use en.cppreference.com/w/cpp/algorithm/random_shuffle Commented Mar 5, 2020 at 10:57
  • I am meant to create my own algorithm without using any shuffle functions Commented Mar 5, 2020 at 10:57
  • 1
    The brute force solution is quite easy. Do you care about performance? If yes you should sort the data, to be more efficient in removing duplicates. Then shuffle it again. It may be good to write 3 seperate functions for this. Commented Mar 5, 2020 at 11:04
  • 1
    @ZachSaw be cool buddy, He is asking for help, we all go through this kind of thing. Commented Mar 5, 2020 at 11:12

3 Answers 3

1

You should swap elements in this line numArray[i] = numArray[RandomIndex];, not assigning. This will duplicate data! Here's the swap:

int v = numArray[i];
numArray[i] = numArray[RandomIndex];
numArray[RandomIndex] = v;
Sign up to request clarification or add additional context in comments.

Comments

1

You are duplicating the elements with the assignement inside the for loop

numArray[i] = numArray[RandomIndex]

Instead asign the element to the position of the array, you need to swap those elements as follow:

class numbers
{
  int randomIndexCount;
public:
  void randomArray (int numArray[], int size)
  {
    randomIndexCount = 0;
    // Use srand with a time seed value in order to
    // have different results in each run of the programm
    srand (time (NULL));
    for (int i = 0; i < size - 1; i++)
      {
    int swap = numArray[i];
    //take a random index from 0 to i  
    int j = rand () % (size);
      numArray[i] = numArray[j];
      numArray[j] = swap;
      cout << numArray[i] << endl;
      }
  }
};

int main ()
{
  int numArray[9] = { 1, 2, 3, 4, 5, 6, 0, 0, 0 };
  numbers n;
  n.randomArray (numArray, 9);
  return 0;
}

This will be the output that includes all then numbers in the array:

5
2
0
6
3
0
1
0

2 Comments

The output should contain all the numbers in the array in a random order 1-6 and 0,0,0
tested it out and still has duplicate numbers and only prints out 8 numbers instead of 9
1

This loop scans positions from the end of the array towards its beginning and randomly selects a new item to be put at the current position. Items are chosen from positions not scanned yet.

This way every place is exactly once chosen to be filled with some item, and each item is exactly once placed in its final position (although, before this happens, it may be several times swapped out of places chosen for other elements).

It also guarantees no item disappears (gets overwritten) and no duplicates appear (no item is copied inadvertently) - if you have duplicates in input data, the same duplicates remain in the output (although permuted); if there are no duplicates, there will be no duplicates.

Additionally, if the rand() function has no bias, every item has the same chance to end at any chosen position, hence each possible permutation is equally probable as an output.

for (int i = size; i > 1; -- i)
{
    int swapIndex = rand() % i;

    int swap = numArray[swapIndex];
    numArray[swapIndex] = numArray[i-1];
    numArray[i-1] = swap;
}

3 Comments

this code is half correct, it removed duplication however only prints out 8 elements not 9
@willzz100 This code does print nothing, it only permutes an array.
@willzz100 I have expanded a description of the loop's properties, hopefully it is useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.