• c++ word inventor
    44 replies, posted
Hey ! After a lot of experimenting and learning some things about unidimensional arrays, an ideea struck me : make a word inventor. Here's a link to my blog with explanations and a download link. Yes I know it's in romanian, but all you have to do is type the number of characters you want the word to be, and it will generate the word ( it takes aprox 1 sec for each character to generate). [url]http://sjackm.wordpress.com/2009/10/19/the-program-that-makes-words-c/[/url]
Sounds like a fun time killer. It's wrong section, though. Try the [url=http://www.facepunch.com/forumdisplay.php?f=240]programming forum[/url]. Other than that cool program, will try it out shortly. I like that you actually thought through how to make it generate "non-nonsense" (read the article at your homepage).
bum rape bum rape bum rape bum rape bum rape [highlight](User was banned for this post ("Spam" - cosmic duck))[/highlight]
Oh man, I'm extremely sorry. I'm just new to these forums and they're a bit confusing to me. Any admin that sees this, please move my post to that section.
It doesn't work for me. As soon as I open it, it stops responding and I have to close it.
Pretty interesting... here's some words I got: [code]nquxx awaagen eadu eto iniuxa iruee exoeheriyu efiiu oilovuceilusaciimisicoimosozuimusrz[/code]
It has its problems, but it's otherwise pretty fun.
[quote]oilovuceilusaciimisicoimosozuimusrz[/quote] I lol'd. How many seconds did it take ? =)) [b]Within[/b], I don't know why it's crashing on your system. Are you not using Windows ? If so, it won't work because it includes a windows library. [EDIT] If you consider it funny, then I would like to hear your ideas improving it :D. Also thanks admin for moving my post !
What do you need the Windows API for in this program ô.o Maybe you could post the source code? Also, crashes for me too using Windows 7 64-bit. I was able to run other 32-bit programs without any problems so far though.. but I am using this OS just since last week. As such I don't know if it is already in there, but you could ask how many words it shall produce at once, with how many minimum and maximum characters.
Yeh crashes for me too on Win 7. Can you maybe post the code?
So it prints out a random list of characters?
[QUOTE=garry;17917180]So it prints out a random list of characters?[/QUOTE] Excuse me, Mr. Newman, I'm afraid I have to inform you that you're wrong. [editline]06:23PM[/editline] [QUOTE=SamuraiJack;17916695] [b]Within[/b] are you not using Windows?[/QUOTE] I'm using Vista 64-bit, by the way.
It looks like it follows a bit of rules to make something pronounceable. The biggest issue I see is why does it take so long for something that should be generated in linear time?
[QUOTE=Dragon;17917247]It looks like it follows a bit of rules to make something pronounceable. The biggest issue I see is why does it take so long for something that should be generated in linear time?[/QUOTE] Sleep function from the Windows API :sigh: (Since he's already using the Windows API in his code, he should've used SetConsoleTitle and SetConsoleTextAttribute instead of system("title etc") and system("color 20")) @op, you should post the code.
I could imagine doing this as a sort of probability function for choosing the next letter based upon a 3 or 4 character window of previous characters generated. For example, 3 vowels in a row should be incredibly rare, and never all 3 being the same. Judicial use of [I]th, gh, wr[/I], and other compound sounds so you can hopefully try to make something pronounceable. Try to construct it one syllable at a time.
[QUOTE=Cathbadh;17917516]I could imagine doing this as a sort of probability function for choosing the next letter based upon a 3 or 4 character window of previous characters generated. For example, 3 vowels in a row should be incredibly rare, and never all 3 being the same. Judicial use of [I]th, gh, wr[/I], and other compound sounds so you can hopefully try to make something pronounceable. Try to construct it one syllable at a time.[/QUOTE] I really don't think he's targeting English...
[quote]The biggest issue I see is why does it take so long for something that should be generated in linear time? [/quote] That's because I tried to make it instantly, but the seed for the rand function is the system time, and if it makes it instantly all the characters will be aprox the same. I don't know how to generate a lot of random numbers instantly :( . Let me translate the code, and I'll post it. brb [EDIT] Yes, I was not targeting english. For romanian, the rule with consonants and vowels is happily enough. In our language, the words are read as they are.
[QUOTE=jA_cOp;17917538]I really don't think he's targeting English...[/QUOTE] Then that makes it exponentially simpler. English is like the x86 of spoken languages. Full of weird hacks and inconsistencies, and yet everyone uses it.
You should be seeding only once.
The words aren't supposed to be *that* realistic I think. Also - you only need to seed rand once, you realise: after that, the algorithm for the random number generator is specifically designed to create numbers that seem completely random very fast.
[QUOTE=SamuraiJack;17917560]That's because I tried to make it instantly, but the seed for the rand function is the system time, and if it makes it instantly all the characters will be aprox the same. I don't know how to generate a lot of random numbers instantly :( . Let me translate the code, and I'll post it. brb [EDIT] Yes, I was not targeting english. For romanian, the rule with consonants and vowels is happily enough. In our language, the words are read as they are.[/QUOTE] You only need to set the seed once
[QUOTE=SamuraiJack;17917560]That's because I tried to make it instantly, but the seed for the rand function is the system time, and if it makes it instantly all the characters will be aprox the same. I don't know how to generate a lot of random numbers instantly :( . Let me translate the code, and I'll post it. brb[/QUOTE] No, that's not how it works. When seeding with the system time, reseeding with a new time is useless. If you only seed once, you'll find your numbers plenty random: [cpp] srand(time(NULL)); for(int i = 0; i < 100; i++) { printf("%d\n", (rand()%100) + 1); } [/cpp]
Someone should tell him he only needs to seed once
I did something similar a long time back. It used phonemes (as discussed [url=http://www.flipcode.com/archives/Generating_Names_Phonetically.shtml]here[/url]) to make it seem somewhat natural. Didn't seem like English or any other language I know, though. [quote]foudgge piychou showm moyvow nga boyph dongee soechke zoaph si[/quote] Could probably be better if I made some kind of rule system.
I'm done with translating. Here's the code. I assume I don't have to repost the translated program, as all of you who discuss here have a c++ compiler :) [code]#include <iostream> #include <windows.h> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; //randomization function called if a vowel must be randomly choosed int vocalrand(){ int v; srand(time(NULL)); v=rand()%5; cout<<"V is equal to "<<v<<" "<<endl; cout<<" "<<endl; return v; } //randomization function that helps randomly choose a character from the 26 available int randomi(){ int n; srand(time(NULL)); n=rand()%26; cout<<" N is equal to "<<n<<" (the number of the character in the array) "<<endl; cout<<" "<<endl; return n; } int main(){ system("title Generatorul de Cuvinte"); // program name system("color 20"); // system color int n,contor,rand_liter,rand_vocal,conson,restart; // n=nr of characters, contor is used to move to the next character, rand_liter is used for the random character generation, rand_vocal generates a random vowel, conson is used to verify if there are more than 2 consonants and the next character is still a consonant ,restart is for checking the reset condition double secunde; char cuv[n]; char *liter="abcdefghijklmnopqrstuvwxyz"; // the 26 characters array ( or string ? don't really know, I'm a beginner. But I know it works, and I spent 1 hour trying to make a 26 character array :D) cout<<" Type the number of characters the word will have"<<endl; cout<<" "<<endl; cout<<" The text that's generated while the characters are generated is not important"<<endl; cout<<" "<<endl; cout<<" The optimal number of characters is between 7 and 10 , under 5 the word won't always be readable and more than 15 will make it unreadable "<<endl; cout<<" "<<endl; cout<<"Number of characters:"<<endl; cin>>n; secunde=n*900/1000; cout<<" "<<endl; cout<<"You'll have to wait "<<secunde<<" seconds for the word generation"<<endl; cout<<" "<<endl; cout<<" "<<endl; cout<<" "<<endl; contor=0; while(contor<n){ // the code that does the job starts here. ok, please don't scream OMG when you see the if condition, I know I could have used negative checks and it would be a lot smaller, but I realised that too late and was too lazy to delete the line and redo it :P rand_liter=randomi(); if((rand_liter==1)||(rand_liter==2)||(rand_liter==3)||(rand_liter==5)||(rand_liter==6)||(rand_liter==7)||(rand_liter==9)||(rand_liter==10)||(rand_liter==11)||(rand_liter==12)||(rand_liter==13)||(rand_liter==15)||(rand_liter==16)||(rand_liter==17)||(rand_liter==18)||(rand_liter==19)||(rand_liter==21)||(rand_liter==22)||(rand_liter==23)||(rand_liter==24)||(rand_liter==25)){ ++conson; cout<<" conson++ "; } if(conson>=2){ cout<<" 2 consons true "<<endl; Sleep(500); rand_vocal=vocalrand(); conson=0; switch(rand_vocal){ case 0: cuv[contor]='u'; break; case 1: cuv[contor]='a'; break; case 2: cuv[contor]='e'; break; case 3: cuv[contor]='i'; break; case 4: cuv[contor]='o'; break; }} else{ cuv[contor]=liter[rand_liter]; } ++contor; cout<<"====================================================="<<endl; cout<<" "<<endl; Sleep(900); } cout<<" "<<endl; cout<<" "<<endl; cout<<" The generated word is: "<<endl; cout<<cuv<<endl; cout<<" "<<endl; cout<<" The weird characters from the end of the word are not important"<<endl; cout<<" "<<endl; cout<<"|-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-|"<<endl; cout<<" "<<endl; cout<<" CopyRight SamuraiJack -> marcel_max2006@yahoo.com, 2009 "<<endl; cout<<" "<<endl; cout<<"Try again ?(yes=1, no=0) "<<endl; cin>>restart; if(restart==1){ system("CLS"); return main(); } else{ return 0;} } [/code] [b]jA_cOp[/b], I think I should talk with you about the seeding problem on an instant messaging program please. MSN, Skype, Yahoo. What do you prefer ? Only if you can :). I tried seeding it only once and it makes the same number every time :\.
Sorry, what? * 900/1000?? Why not just * 0.9 - You've replaced two ops with one there?
[QUOTE=SamuraiJack;17917856][b]jA_cOp[/b], I think I should talk with you about the seeding problem on an instant messaging program please. MSN, Skype, Yahoo. What do you prefer ? Only if you can :). [/QUOTE] I prefer the forums. [QUOTE=SamuraiJack;17917856]I tried seeding it only once and it makes the same number every time :\.[/QUOTE] The snippet I posted works fine. [cpp] #include <stdio.h> #include <time.h> #include <stdlib.h> int main() { srand(time(NULL)); int i; for(i = 0; i < 100; i++) { printf("%d\n", (rand()%100) + 1); } return 0; } [/cpp] Prints 100 random numbers between 1 and 100.
I don't really understand why you use printf instead of cout, and printf seems to be weird to me. Also, what does "%d\n" because I'm guessing it's an active element not displayed text. I'm sorry, I didn't learned programming as this and I'm new to this "printf" element.
It's just C code, not C++. printf is what people use(d) instead of std::cout, %d would interpret the argument as an integer and \n prints a new line.
What he's trying to say is that you should seed *once* at the beginning of your program, and never after. This will probably work in your case, although you use a recursive main (which is a big no-no).
Sorry, you need to Log In to post a reply to this thread.