Quick Question

Aug 25, 2009 at 5:48pm
Is there a function that randomly shuffles up an array?
Say I make a char array with the alphabet in it, and I want it to randomly print Letters from the char array. Thanks!
Aug 25, 2009 at 5:51pm
Aug 25, 2009 at 5:57pm
Ok thanks =P
Aug 25, 2009 at 6:09pm
I'm not understanding it.
D=
Aug 25, 2009 at 6:11pm
What do you don't understand? Post more specific question(s).
Last edited on Aug 25, 2009 at 6:12pm
Aug 25, 2009 at 6:30pm
I can't understand how the code works, and what the syntax means.
I'm still learning things.
Aug 25, 2009 at 6:42pm
ok, this should get you started :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>

int main() {

    char alphabet[26];
    for ( int i = 0; i < 26; ++i )
        alphabet[i] = i + 'a';

    std::random_shuffle(alphabet, alphabet+26);

    for ( int i = 0; i < 26; ++i )
        std::cout << alphabet[i] << ' ';

    return 0;
}


EDIT : edited a typo in code
Last edited on Aug 25, 2009 at 7:47pm
Aug 25, 2009 at 6:55pm
You mean

 
std::random_shuffle( alphabet, alphabet + 26 );


Topic archived. No new replies allowed.