vendredi 24 avril 2015

How to fetch 'n' random values out of an array c++

i would like to fetch 6 random values out of an array. i can do it in Python using x=random.sample(listName, numberOfValuesNeeded)

[code]
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
const int MAX_Name=100;

srand((unsigned)time( NULL));

// Names
string dispNames[MAX_Name]={"Alberto", "Bruno", "Carlo", "Dario", "Elio", "Francesco", "Giovanni", "Luca", "Marco", "Nicola", "Oreste", "Pietro",   "Rino", "Sandro", "Tonino", "Valerio", "Vittorio"};


    for(int x=1; x<=6; x++)
    {
        random_shuffle(begin(dispNames), end(dispNames));

        cout << x << ": " << endl; // snomething missing before endl
    }

    return 0;
}

I want to print on the screen: 1: random name1 2: random name3 ... 6: random name6




Aucun commentaire:

Enregistrer un commentaire