+ 4

Help! Random Char Generator

I initialized a char array. How can I ensure that the five characters selected randomly with( rand() ) from a character array are different?

16th May 2018, 12:08 PM
Ali B.
Ali B. - avatar
5 Respostas
+ 10
As I prescribed. #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<string> #include<ctime> using namespace std; void TeamGen(); int main() { TeamGen(); } void TeamGen() { srand(time(NULL)); string goalk[]= {"Neuer","Ederson","Cladio Bravo","Sergio Romero","Hugo Lloris","Simon Mignolet","Willy Caballero", "Peter Cech","David Ospina","Jasper Clissen","Jan Oblak","Keylor Navas", "Neto","Sergio Asenjo", "Sve Ulreich","Ralf Fahrmann","Oliver Baumann","Roman Burki","Roman Weidenfeller","Berd Leno", "Wojciech Szczesny","Buffon","De Gea","Curtuois","Ter Stegen","Donnarumma","Karius",}; string def[]= {"Ramos","Puyol","Pique","Albiol","Vermaelen","Skrtel","Van Dijk","Lovren","Diego Godin", "Filipe Luis","Rolando","Adil Rami","Pepe","Medel","Damogaj Vida","Yuto Nagatomo","Joao Miranda", "Davide Santon","Vincent Kompany","John Stones","Aymeric Laporta","Chris Smalling","Luke Shaw", "Eric Bailly","Jan Vertonghen","Toby Alderweireld ","Kyle Walker","Nathaniel Clyne","David Luiz", "Gary Cahill","Laurent Koscielny","Anthony Rudiger","Hector Bellerin","Per Mertasacker", "Phil Jagielka","Eliaquim Mangala",}; string mid[]={"Salah","Paulinho","Ozil","Iniesta","Xavi","Coutinho","Gerard","Ronaldinho","Kroos","Draxler"}; string forw[]={"Messi","Ronaldo","Suarez","Firmino","Ibrahimovic","Diego","Rashford"}; for (int i = 0; i < 30; i++) { swap(def[rand()%30],def[rand()%30]); swap(mid[rand()%9],mid[rand()%9]); swap(forw[rand()%7],forw[rand()%7]); } cout<<"GK:"<<endl; cout<<goalk[rand()%7]<<endl; cout<<"DEF:"<<endl; for(int i=0;i<4;i++) { cout<<"-"<<def[i]<<endl; } cout<<"MID:"<<endl; for(int i=0;i<4;i++) { cout<<"-"<<mid[i]<<endl; } cout<<"FOR:"<<endl; for(int i=0;i<2;i++)
17th May 2018, 2:00 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
You can use std::swap from <algorithm>, combined with rand() to randomly swap slots of the char array values. #include <algorithm> #include <cstdlib> #include <ctime> int main() { srand(time(0)); char arr[5] = {'a', 'b', 'c', 'd', 'e'}; for (int i = 0; i < 10; i++) std::swap(arr[rand()%5], arr[rand()%5]); } Selecting characters from the array can then be done linearly, resulting in non-repetitive values, since the values are just in random order, and not duplicated.
16th May 2018, 12:24 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
And Finally Hatsy Rei has helped you to take code of the day
26th May 2018, 4:12 AM
Mohamed Gadaphy Nkwenkwat
Mohamed Gadaphy Nkwenkwat - avatar
+ 2
try to 1) put the random characters generated for goalk, def , mid and forw inside a new array 2) put a conditional statement to check duplicates inside the new array 1) if duplicates: conditional statement will remove the duplicates 2) define a new array inside conditional statement for entry for non-duplication values only 3)print back the new non-duplicated array hope this works! !HAPPY CODDING!
17th May 2018, 3:38 AM
Rajeeb
+ 1
Thank you for your attention. But my problem is here https://code.sololearn.com/c1fiBu1VZ9CV/?ref=app in this code I try making football team randomly. When I generating defenders position, for example Puyol generated two times for one team.This shouldn't be because you know doesn't happen one person two times in team. So, how can I prevent this issue?
16th May 2018, 7:20 PM
Ali B.
Ali B. - avatar