Skip to content

Instantly share code, notes, and snippets.

@pointbazaar
Last active October 30, 2017 02:28
Show Gist options
  • Save pointbazaar/067cc1e11a77ae65892cdbc0bfb3b298 to your computer and use it in GitHub Desktop.
Save pointbazaar/067cc1e11a77ae65892cdbc0bfb3b298 to your computer and use it in GitHub Desktop.
shuffle a list of strings randomly
#include <string>
#include <random>
#include <iostream>
#include <time.h>
using namespace std;
int main(){
cout << "bitte geben sie stringzahl ein"<<endl;
int x;
cin >> x;
string Liste[1000];
string Lesen;
vector<string> randem;
srand(time(0));
cout << "Geben sie bitte die namen ein"<<endl;
for(int i=0;i<x+1;i++){
getline(cin,Lesen);
Liste[i]=Lesen;
}
int istartnorandom=1;
while(istartnorandom<(x)){
string temp=Liste[istartnorandom];
int randomindex= (rand()%(x-istartnorandom))+istartnorandom+1;
cout << "randomindex: " << randomindex;
Liste[istartnorandom]=Liste[randomindex];
Liste[randomindex]=temp;
istartnorandom++;
}
cout << endl << "----------" << endl;
for(int i=1;i<x+1;i++){
cout << Liste[i] << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment