2

I'm attempting to implement the printRandom() function which should repeatedly select and remove a random entry from the array C and then prints out the selected value until all values in the array C have been printed. They can only be printed one time each.

I can only use cstdlib and iostream. This is my code for the function thus far:

void printRandom(int C[], int n, int seed) {
srand(seed);
int test[10] = { 21,-34,2,-42,89,24,11,4,13,-18 };

for (int i = 0; i < 10; i++)
{
    int r = rand() % 10;
    for (int j = 0; j < 10; j++) {
        if (r != C[j]) {
            C[i] = test[r];
            cout << C[i] << ' ';
        }
    }

}
}

This is the main function I am working with:

int main() {
  int A[10], B[10] ;

  A[0] =  21 ;
  A[1] = -34 ;
  A[2] =   2 ;
  A[3] = -42 ;
  A[4] =  89 ;
  A[5] =  24 ;
  A[6] =  11 ;
  A[7] =   4 ;
  A[8] =  13 ;
  A[9] = -18 ;

  for (int i=0 ; i < 10 ; i++) {
     B[i] = A[i] ;
  }
  printRandom(B,10,38173410) ;

  for (int i=0 ; i < 10 ; i++) {
    B[i] = A[i] ;
 }
  printRandom(B,10,83103131) ;

 for (int i=0 ; i < 10 ; i++) {
    B[i] = A[i] ;
 }
  printRandom(B,10,77192102) ;

return 0 ;
}

Right now I'm getting a large block of numbers

-34 -34 -34 -34 -34 -34 -34 -34 -34 -34 24 24 24 24 24 24 24 24 24 24 13 13 13 13 13 13 13 13 13 13 89 89 89 89 89 89 89 89 89 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 2 2 2 2 2 2 2 2 2 -42 -42 -42 -42 -42 -42 -42 -42 -42 -42 4 4 4 4 4 4 4 4 4 4 13 13 13 13 13 13 13 13 13 13 -18 -18 -18 -18 -18 -18 -18 -18 -18 -18 -34 -34 -34 -34 -34 -34 -34 -34 -34 -34 2 2 2 2 2 2 2 2 2 -42 -42 -42 -42 -42 -42 -42 -42 -42 -42 4 4 4 4 4 4 4 4 4 4 11 11 11 11 11 11 11 11 11 11 -34 -34 -34 -34 -34 -34 -34 -34 -34 -34 -42 -42 -42 -42 -42 -42 -42 -42 -42 -42 21 21 21 21 21 21 21 21 21 21 -42 -42 -42 -42 -42 -42 -42 -42 -42 -42 13 13 13 13 13 13 13 13 13 13 -34 -34 -34 -34 -34 -34 -34 -34 -34 -34 89 89 89 89 89 89 89 89 89 -42 -42 -42 -42 -42 -42 -42 -42 -42 -42 13 13 13 13 13 13 13 13 13 13 -42 -42 -42 -42 -42 -42 -42 -42 -42 -42 13 13 13 13 13 13 13 13 13 13 89 89 89 89 89 89 89 89 89 89 13 13 13 13 13 13 13 13 13 13

I took some time off of C++ and was wondering if you could help or at least point me in the right direction. Thank you!

Alicia Sabatino
  • 57
  • 1
  • 2
  • 10
  • 3
    I think you want a [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). – Fred Larson Sep 19 '17 at 14:11
  • 1
    you can use [`std::random_shuffle`](http://en.cppreference.com/w/cpp/algorithm/random_shuffle) – 463035818_is_not_an_ai Sep 19 '17 at 14:13
  • 1
    @tobi303: Yes, except "I can only use cstdlib and iostream." – Fred Larson Sep 19 '17 at 14:14
  • sorry didnt see that. In any case you should avoid rolling random numbers in the full range and only use them if you didnt use them yet. For large input this has horrible impact on efficiency. Consider an array of size 100 (still small), then you will need in the order of 100 rolls to get the last valid random index and roughly 50 for the second last. – 463035818_is_not_an_ai Sep 19 '17 at 14:20
  • 2
    This test, `if (r != C[j])`, doesn't make any sense. `r` is an index, but you're comparing it to a value in the array. – Michael Burr Sep 19 '17 at 14:21
  • @FredLarson thank you! I like how this works. However, I'm not allowed to edit the parameters of the printRandom function to include a pointer. Is there a way around this? Thanks again! – Alicia Sabatino Sep 19 '17 at 14:24
  • @tobi303 from what I can tell we are supposed to roll through. We aren't really supposed to use anything other than cstdlib and iostream and since it's just an array of ten numbers its okay, however, I see your point. Practically, this is not efficient. – Alicia Sabatino Sep 19 '17 at 14:26
  • @AliciaSabatino if you can do the same with a factor of 100 less operations you should go for it, even if it is just a small array – 463035818_is_not_an_ai Sep 19 '17 at 14:29
  • @AliciaSabatino: You have a pointer, but it's disguised in array syntax. `C` is really a pointer.See https://stackoverflow.com/q/24041263/10077. – Fred Larson Sep 19 '17 at 14:32
  • @FredLarson perfect! thank you so much! a little rusty with pointers I've taken a few years off! thank you again – Alicia Sabatino Sep 19 '17 at 14:34
  • 2
    @tobi303 Do note that `std::random_shuffle` is deprecated currently and will be removed in C++17. `std::shuffle` is the better option to use. – NathanOliver Sep 19 '17 at 14:42

1 Answers1

1

Apart from possible bugs your method is rather inefficient, as for example you roll random numbers a lot of times to find the last missing element, when this actually does not require a dice roll at all.

You can avoid this "reroll until index is valid" if you keep track of what indices have been used already. In pseudo code:

void printRandom(int C[], int n, int seed) { 

     D = C;
     size = n;

     for i = 1:n {
          x = rand(  [0... size)  );

          print element at D[x]

          D[x] = D[size-1];

          size--;
     }
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185