C++ randomizing string help
Can someone please tell me or show me how to take a word and randomize it. Like to take the word moo and randomize it to make it oom and so on. I am new to programming and would like to make a word descrambler where it takes a word and compares it to a text file then randomize it and checks again until it finds the right word. I have already made it so that it checks the file for the word but I cant figure out how to randomize the word. I don't no if I'm supposed to change it into a char or what. Thanks to all that help.
O ya im using Dev C++ and on windows vista if that helps. Thanks
Re: C++ randomizing string help
Code:
#include <iostream>
#include <algorithm>
#include <string>
void print_all_permutations(std::string s)
{
std::sort(s.begin(), s.end());
do
{
std::cout << s << std::endl;
} while (std::next_permutation(s.begin(), s.end()));
}//print_all_permutations
int main()
{
print_all_permutations("dog");
return 1;
}//main
gg
Re: C++ randomizing string help
Thanks a lot for helping but this prints all of them at once. Is there a way to make this print only one so that i can compare it and if there is not a match then go on to the next? Thanks a lot for helping again :).
Re: C++ randomizing string help
^I should think the method would be fairly apparent from the above code sample.
While I'm a big preponent of the std::string class, let's face it, they don't have anything as funny as this:
Quote:
The function below addresses the perennial programming quandary: “How do I take good data in string form and painlessly turn it into garbage?” This is actually a fairly simple task for C programmers who do not use the GNU C library string functions, but for programs based on the GNU C library, the strfry function is the preferred method for destroying string data.
Re: C++ randomizing string help
Ya it would be good if i could get it to print all the possibilities to a text file and just compare the text files but I cant get to seem to make it do that. I try adding the ofstream which i learned from cplusplus.com but i just get a whole bunch of errors every time I try to change anything in his code.
Re: C++ randomizing string help
You should be aware that the algorithm you're proposing is, in a word, slow. Consider the word "algorithm", of which there are 9! = 36,2880 permutations. Would you want to take each of those permutations and compare it (string comparison has its own complexity, too) against each word in a dictionary file?
How about you tally the number of times each letter of the alphabet occurs in a word, and compare that to a similar count for each word in your dictionary.
Re: C++ randomizing string help
Quote:
Originally Posted by Hellboyz
Like to take the word moo and randomize it to make it oom and so on.
To randomize a word - shuffle it. There's a very simple standard algoritm available for that. You'll find it if you search the net.
Re: C++ randomizing string help
I always do a lot of searching before posting anything. But I can't find any algorithm for shuffling up a string and thanks Hermit I thought that it wouldn't take that long because of how fast a computer could go but Ill just say exactly what I'm trying to do. I've read some books on programming in C++ and I'm still learning but if any of you know what Hack this site is they have some programming challenges. I'm trying to actually accomplish this one the hard way by doing it myself rather then just using someone else's program to do it for me. What I have done so far is to assign all my scrambled words to variables and run through the program to check if they are a match. I just can't figure out how to randomize the strings yet. So with that said what kind of function or thing would I do for my specific situation? Thanks.
Re: C++ randomizing string help
Were you after something like this?
http://www.cppreference.com/wiki/stl...random_shuffle
You should be able to look at the source to get an idea of how it is done.
Re: C++ randomizing string help
Quote:
Originally Posted by Hermit
How about you tally the number of times each letter of the alphabet occurs in a word, and compare that to a similar count for each word in your dictionary.
That would be a *much* faster way to tell if two words are identical "up to permutations".
Quote:
I thought that it wouldn't take that long because of how fast a computer could go
Computer speed is great. Complexity theory is still incredibly important, though. A combinatorial function grows *much* faster than processor speed increases.
Re: C++ randomizing string help
What kind of suggested combinations or permutations to code by a stupid student ?
What algorithm using by STL ?
For instance, std::sort() used IntroSort() - QuickSort, HeapSort and InsertionSort, I mean what algorithm in random_shufftle, next_permutation and random_sample ?
I just want to learn the algorithm and coded it myself.
A billion thanks for your help.
Re: C++ randomizing string help
Re: C++ randomizing string help
Here is the source for HP's random_shuffle
Code:
template <class RandomAccessIterator, class Distance>
void __random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
Distance*)
{
if (first == last) return;
for (RandomAccessIterator i = first + 1; i != last; ++i)
#ifdef __STL_NO_DRAND48
iter_swap(i, first + Distance(rand() % ((i - first) + 1)));
#else
iter_swap(i, first + Distance(lrand48() % ((i - first) + 1)));
#endif
}
template <class RandomAccessIterator>
inline void random_shuffle(RandomAccessIterator first,
RandomAccessIterator last)
{
__random_shuffle(first, last, distance_type(first));
}
Re: C++ randomizing string help
What is the algorithm used by this STL ?
Re: C++ randomizing string help
Quote:
What is the algorithm used by this STL ?
From what I see, that standard library implementation uses (a variant of) Knuth shuffle, which is known by other, perhaps more accurate, names.