-
program help
I am writing a program that takes the alphabet and randomly mixes it up and compares it to the alphabet (you have to compile it to fully understand) i got as far as making a random mixed alphabet but sometimes letters are used more than once, what do i put in to make sure that no letter is used more than once?
here's what i got so far
Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;
void crypt();
int rand(int n);
char *alphabet[26] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
int main() {
int i, n;
srand(time(NULL));
while(1) {
cout<< "type 1 start type 0 to exit"<<endl;
cin>> n;
if (n == 0)
break;
if ( n == 1) {
for (i=1; i<=26; i++)
crypt();
cout<<endl;
cout<< "a b c d e f g h i j k l m n o p q r s t u v w x y z"<<endl;
}
}
return 0;
}
void crypt() {
int a;
a = rand(26);
cout<< alphabet[a]<<" ";
}
int rand(int n)
{
return rand() % n;
}
-
Code:
char[] Alphabet = "abcdefghijklmnopqrstuvwxyz";
std::random_shuffle(Alphabet, Alphabet + 26); // located in <algorithm>
Besides, you should use <cstdlib>, <ctime> and <cmath> over their deprecated .h alter-egos.
Edit: Can I also mention that you aren't using anything out of both the math.h and stdlib.h header files ?
-
srand() and rand() are both in stdlib
-
You don't randomly mix up the letters at all, you just pick random letters out of them. You may very well get lots of repeats.
Example:
Code:
[a, b, c, d, e]
rand(5) = 6 % 5 = 1 (a)
rand(5) = 17 % 5 = 2 (b)
rand(5) = 1 % 5 = 1 (a) //repeat!
You'll need to make sure letters which have already been printed can never be choosen again. You could shuffle the array, as suggested; it's prolly the simplest way. You could also add tokens to letters (eg. a second list of booleans) if they have already been printed, and check letters for existing tokens before printing them again. Slow and hackish though.
-
(i'm pretty new to c++) Can somebody explain how to use the shuffle array thing?
-
-
On many implementations, before you call random_shuffle you need to call srand once to seed the random number generator just like you would for rand(). You also have to #include <algorithm>. Then, you call random_shuffle just like Desolation showed, with the first argument being the start of the array and the last being one past the end.
If you call random_shuffle once, it will shuffle everything in your array. Then you can go about comparing it to the actual alphabet like you had planned.
-
And some other popular implementations of random_shuffle() doesn't use rand() and srand() at all ( e.g. the one that comes with g++ 3.3.5 ).
in that case you have to write your own RandomGenerator function object to be able to seed the random function.
e.g.
Code:
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
struct rndgen {
int operator ()( int num ) {
static bool isinitialized = false;
if ( ! isinitialized ) {
srand(time(0)); isinitialized= true;
}
return rand() % num;
}
};
int main() {
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
rndgen r;
random_shuffle(alphabet, alphabet+strlen(alphabet), r );
cout << alphabet << endl;
}
Kurt
-
The random_shuffle is probably better. If you wanted to write your own, you might want to just try picking 2 random numbers between 0 and 25 and just swapping the letters at those indexes.
Code:
char alphabet[]="abcdefghijklmnopqrstuvwxyz";
char temp;
int random_num1=rand()%25;
int random_num2=rand()%25;
temp=alphabet[random_num1];
alphabet[random_num1]=alphabet[random_num2];
alphabet[random_num2]=temp;
rinse and repeat.....
-
If you want to write it yourself the algorithm is pretty simple although it is easy to get wrong. You start from the end and pick a random number from 0 to the that index, swapping with the value at the random index. Then move to the next index down and continue until there is only one index left.
-
Daved: It's called the Knuth shuffle (or Fisher-Yates shuffle).