Member Avatar for Geek-Master

I'd like to ask if anyone knows how to randomize character variables, and if it is possible to define the possible characters it can use in the randomizing process. Any info would be excellent ^^ T#4NK$.

You can load your characters into a vector and use something like:

random_shuffle(cV.rbegin(), cV.rend());

Check out the code snippet on vectors to get an idea how to create and display the vector.

Member Avatar for Geek-Master

I'm still new to programming so I'll read up on vectors and your code, thanks by the way...but could I trouble you on what header to include to use that line of code? Thanks again.

I'm still new to programming so I'll read up on vectors and your code, thanks by the way...but could I trouble you on what header to include to use that line of code? Thanks again.

... kind of slapped this together without testing it, should work ...

// Dev-C++

#include <iostream>
#include <algorithm>  // check stl_algo.h for details
#include <vector>     // vector header

using namespace std;  // std::cout etc.

int main()
{
  int k;

  // another way to load a vector ...
  vector<char> cV(50, '-');
  cout << "this vector has 50 char - :\n";
  for(k = 0; k < cV.size(); k++) 
    cout << cV[k]; 
  cout << endl;
  
  // insert some additional elements
  cout << "inserted 10 char c starting with element 25:\n";
  cV.insert(cV.begin() + 25, 10, 'c');   
  for(k = 0; k < cV.size(); k++) 
    cout << cV[k]; 
  cout << endl;
  
  // take the character vector and shuffle the characters
  cout << "let's shuffle the thing ...\n";
  random_shuffle(cV.rbegin(), cV.rend()); 
  for(k = 0; k < cV.size(); k++) 
    cout << cV[k]; 
  cout << endl;  

  cin.get(); // wait  	
  return 0;
}
Member Avatar for Geek-Master

Yes it did work, thanks :cheesy: . I shall "digest" this code to master C++ a little more...oh yeah ^^.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.