Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Shuffle Array always outputs same shuffled result

+2 votes
asked May 12, 2021 by Alex Anderson (200 points)

In greater detail: I've spent the majority of the day attempting to write a program that will rearrange an array of numbers.

Eventually, after much trial and error on my own, I found the shuffle_array function online, which works great, except for one problem -- it always outputs the same result: 2 0 4 3 1.

I need it to be actually random -- that is, to output a different order of numbers each time I run the program.

Thanks for the help.

#include <bits/stdc++.h>

using namespace std;
 
//Shuffle array
void shuffle_array(int arr[], int n)
{
    //To obtain a time-based seed
    unsigned seed = 0;
    
    //Shuffling our array
    shuffle(arr, arr + n,
            default_random_engine(seed));
 
    //Printing our array
    for(int i=0; i<n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}
 
int main()
{
    int a[5];
    
    for(int assign=0; assign<5; assign++)
    {//This for-loop assigns each slot in the 'a' array its own number
        a[assign] = assign;
    }
 
    int n = sizeof(a) / sizeof(a[0]);
 
    shuffle_array(a, n);
 
    return 0;
}

1 Answer

+1 vote
answered May 12, 2021 by Peter Minarik (55,660 points)
edited May 13, 2021 by Peter Minarik
 
Best answer

There's No Spoon Randomness

Computers are deterministic. They must be, otherwise they couldn't operate. 1 + 1 must be always 2. It cannot be 1 or 3 some times.

The computer has no idea about a random number. What you can generate easily is a "pseudo-random" number. Pseudo, as it's not really random. In fact, easily reproducible (as you've seen yourself).

These pseudo-random numbers make up sequences. They continue forever, there doesn't seem to be any logic for the next number to be generated, nevertheless, these sequences are fully deterministic.

The upside is that there are many such sequences. And by selecting one or the other you may seem to be able to generate "truly" random numbers.

How to select which sequence to use? That's what the seeds are used for.

You can see in your code, you use the seed 0.

If you want to use a (probably) different sequence every time, you can use a different seed every time. A typical solution is to take the current time and use the number of seconds or ticks as shown here.

The Code

#include <chrono>

#include <bits/stdc++.h>

using namespace std;
 
//Shuffle array
void shuffle_array(int arr[], int n)
{
    //Obtaining a time-based seed
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    
    //Shuffling our array
    shuffle(arr, arr + n, default_random_engine(seed));
 
    //Printing our array
    for(int i=0; i<n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}
 
int main()
{
    int a[5];
    
    for(int assign=0; assign<5; assign++)
    {//This for-loop assigns each slot in the 'a' array its own number
        a[assign] = assign;
    }
 
    int n = sizeof(a) / sizeof(a[0]);
 
    shuffle_array(a, n);
 
    return 0;
}

Update

I was bored at work waiting for a build to finish so I looked at this once again.

It seems like shuffle() is part of the c++ standard. There's also an example there (see the bottom of the page) that uses std::vector, instead of a raw array.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...