0

Lets say I wanted the user input to be randomized. Like if the user inputted "1234567890"

The output would be like "5684319027" Or any random number, as the numbers got randomized.

So how would I randomize a user input?

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Akuhyo
  • 23
  • 6
  • 3
    Use [`std::shuffle`](http://www.cplusplus.com/reference/algorithm/shuffle/) – qxz Feb 07 '17 at 22:52
  • @qxz I'm a bit of a noob, so where would I put it in the code? At the beginning, before the input or after? – Akuhyo Feb 07 '17 at 22:53
  • If you get the input as a string, you could then use `std::shuffle` on it, which would rearrange the characters. Then you'd use the string for whatever you need it for. I'll make an answer – qxz Feb 07 '17 at 22:54
  • Alright, thank you. – Akuhyo Feb 07 '17 at 22:55

1 Answers1

0

You can use std::shuffle to randomize the order of elements of pretty much any ordered structure. You can use this on an std::string, which would rearrange the individual characters. Here's an example of usage:

// at the top of your file
#include <algorithm>

// in your main code
std::string str; // define a string variable
std::cin >> str; // get user input into the string

// shuffle the string with a default random engine
std::shuffle(str.begin(), str.end(), std::default_random_engine(std::random_device{}()));

// use the now-shuffled string...
qxz
  • 3,814
  • 1
  • 14
  • 29