I'm working on a project right now where my program reads a text file, and encodes the text file with the character locations from another text file.
Right now I have a multimap, that inputs the characters from the text file, and there locations. What I want to do now is randomly pick one of the numbers from the list of numbers that are related to that character.
For example:
A:0
A:26
A:48
A:128
I want to somehow randomly pick one of those numbers.
Any help would be greatly appreciated.
I suggest that you load all those values into an array - of integers, for example - once that is done generate a random number, modulus it with the size of your array & then that is the number to choose, for example:
I'm trying to do something like the Array but I'm getting an error how would I go about getting this to work?
1 2 3
int myArray[] = bookMap.equal_range('a');
srand(time(NULL));
printf("%d\n", myArray[rand() % (int)bookMap.count('a')]);
I imagine you can see what I'm trying to do, get the numbers that correspond to a into the array, would creating a for loop, and iterating over the equal range then putting the *it into the array work?
Okay, I figured it out, but it's never random, it just seems to use the last element of that type.
1 2 3
multimap<char,int>::iterator a; a = bookMap.equal_range('a').first;
advance( a, rand() % (int)bookMap.count('a') );
cout << "The number is " << a->second << endl;