Random_shuffle: Show the shuffled items on the screen

0

The explanation that comes in cplusplus is difficult for my understanding and I can not understand how this function works (random_shuffle). How to show the shuffled array? Any failure to emphasize?

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<algorithm>

using namespace std;

int const MAX=10;

typedef int tArray[MAX];

int random (int i) { return rand()%i;}

int main()
{
    srand(time(NULL));
    tArray miarray;
    for(int i=0; i<MAX; i++)
    {
        miarray[i]=i;
    }

    random_shuffle(miarray.begin(),miarray.end());
    random_shuffle(miarray.begin(),miarray.end(),random);

    cout << "La secuencia aleatoria es:";

    for()//mostrar los datos barajados
    {
        cout << endl;
    }
}
    
asked by Vendetta 24.02.2018 в 23:02
source

2 answers

0

I put here a version where I try to use your same code, as faithfully as I can, but with a current C ++ style. Look at the comments and if you have doubts that you can not solve with your book (because you need a good book to start, right?) You can put specific questions for each question (I mean the doubts that you can not solve by yourself after a couple of hours of responsible study, right?)

#include<iostream>
#include<ctime>
#include <random> // para default_random_engine()
                  // Generador de números pseudoaleatorios de baja calidad;
                  // probablemente el compilador use rand() internamente

using namespace std; // no es lo más aceptado, pero se usa mucho en ejemplos didácticos

int const MAX = 10;  // no se usan mayúsculas, y en particular "MAX" es un nombre peligroso

typedef int tArray[MAX]; // mejor que "typedef" es "using"


int main()
{

    tArray miarray;
    for (int i = 0; i<MAX; i++)
    {
        miarray[i] = i;
    }

    int seed = static_cast<int>(time(NULL)); // (no uses cast de estilo C)

    shuffle(begin(miarray), end(miarray), std::default_random_engine(seed)); 

    cout << "La secuencia aleatoria es: ";

    for (const auto& i : miarray) { // "range based for loop" se usa muuuucho, acostúmbrate
        cout << i << ' ';
    }
    cout << '\n'; // no uses endl a menos que sea _realmente_ necesario.

    return 0; // no es obligatorio, pero su ausencia puede poner nervioso a algún compañerito de banco.
}
    
answered by 25.02.2018 / 02:39
source
4
  

How to show the shuffled array?

Well, very easy:

for (const auto &valor : miarray) // Mostrar los datos barajados.
{
    std::cout << v << '\n';
}
  

Any failure to emphasize?

  • std::random_shuffle has been marked as deprecated from C ++ 14 . This decision has been taken because the version of std::random_shuffle that uses iterators has a direct dependency with std::rand that is deciding to depress it also , the function std::rand must be replaced by the functions of the header <random> since std::rand is considered harmful.
  • miarray.begin() and miarray.end() do not compile. miarray is an alias of an integer formation ( int ) and as such, has no member functions.
  • A main is missing the return .
  • The use of using namespace std is not advised.
  • In C ++ it is advised that the definitions (variables and symbols) have the smallest possible scope, you should move the alias and the size constant to the inside of main .
  • Using the module operator ( % ) together with std::rand to select random numbers is inaccurate .

    li>

    With the indicated corrections your code could look like this:

    #include <iostream>
    #include <algorithm>
    #include <random>
    
    int main()
    {
        constexpr int MAX = 10;
        using tArray = int[MAX];
        tArray miarray{};
    
        std::generate(std::begin(miarray), std::end(miarray), [i = 0]() mutable { return i++; });
    
        std::random_device dispositivo;
        std::mt19937 generador(dispositivo());
    
        std::shuffle(std::begin(miarray), std::end(miarray), generador); 
    
        for (const auto &v : miarray)
        {
            std::cout << valor << '\n';
        }
    
        return 0;
    }
    
        
  • answered by 25.02.2018 в 00:08