Thread: [Dev-C++] chinese characters
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Jorge G. <jor...@gm...> - 2009-06-16 20:25:03
|
I need to generate random characters for experiment with string matching algorithms , n=size of text nombreArchivo=filename, when n is small the code below generate ascii characters, but when n is greater, (example n =7000000), the code below generate chinesse characters, why? sorry for my english :) void crearArchivo(char* nombreArchivo, int n) { ofstream fSalida; fSalida.open(nombreArchivo); if(fSalida.is_open()) { for(int i=0;i<n;i++) { char c=(char)rand(); fSalida<<c; } } else { cout<<"el arhivo no se pudo crear"; } fSalida.close(); } int main() { crearArchivo("nuevo.txt",1000) ; system("PAUSE"); return EXIT_SUCCESS; } -- Jorge |
From: <Mat...@bm...> - 2009-06-16 23:49:43
|
Hello Jorge, Two things I notice are. 1. In answer to your question about Chinese characters - The rand() function will generate a random number between 0 and 32767 (or more accurately 0 to RAND_MAX) which you are casting into a char. ASCII printable characters reside in the range from 32 to 126. 2. You are not generating random numbers as you expect because you have not set a seed with which to generate random numbers from. Check the function srand. The way it stands now, you will produce the same sequence of characters each time the program is run. The closest approximation to "random" is to use the system clock as a seed as this is different with each execution of the program. void crearArchivo(char* nombreArchivo, int n) { ofstream fSalida; fSalida.open(nombreArchivo); srand(time(NULL)); // Set a random seed if(fSalida.is_open()) { for(int i=0;i<n;i++) { char c=(char)(rand() % 94 + 32); //Only generate numbers in range 32 to 126 fSalida<<c; } } else { cout<<"el arhivo no se pudo crear"; } fSalida.close(); } int main(int argc, char *argv[]) { crearArchivo("nuevo.txt",1000) ; system("PAUSE"); return EXIT_SUCCESS; } Cheers, Matt > From: Jorge Guevara [mailto:jor...@gm...] > Sent: 16 June 2009 21:25 > To: dev...@li... > Subject: [Dev-C++] chinese characters > I need to generate random characters for experiment with string matching algorithms , n=size of text > nombreArchivo=filename, when n is small the code below generate ascii characters, but when n is greater, (example n > =7000000), the code below generate chinesse characters, why? > sorry for my english :) > void crearArchivo(char* nombreArchivo, int n) > { > ofstream fSalida; > fSalida.open(nombreArchivo); > if(fSalida.is_open()) > { > for(int i=0;i<n;i++) > { char c=(char)rand(); > fSalida<<c; > } > } > else > { > cout<<"el arhivo no se pudo crear"; > } > fSalida.close(); > } > int main() > { > crearArchivo("nuevo.txt",1000) ; > system("PAUSE"); > return EXIT_SUCCESS; > } > -- > Jorge This email was sent from an email account of BMW (UK) Manufacturing Limited. BMW (UK) Manufacturing Limited is a company registered in England and Wales with company number 3950868; and has its offices at Ellesfield Avenue, Bracknell, Berkshire, RG12 8TA; registered in England and Wales. The contents of this e-mail are confidential and may be privileged and subject to internal monitoring. If you are not the addressee indicated in this message, you may not copy, forward, disclose, deliver or otherwise use it or any part of it in any form whatsoever. You should then kindly notify the sender by replying to this message and destroy it thereafter. Opinions, conclusions and other information in this message that do not relate to the official business of any BMW Group Company shall be understood as neither given nor endorsed by them. |
From: Jorge G. <jor...@gm...> - 2009-06-17 00:57:08
|
Thanks everybody for yours answers!!! 2009/6/16 <Mat...@bm...> > Hello Jorge, > > Two things I notice are. > > 1. In answer to your question about Chinese characters - The rand() > function will generate a random number between 0 and 32767 (or more > accurately 0 to RAND_MAX) which you are casting into a char. ASCII printable > characters reside in the range from 32 to 126. > > 2. You are not generating random numbers as you expect because you have not > set a seed with which to generate random numbers from. Check the function > srand. The way it stands now, you will produce the same sequence of > characters each time the program is run. The closest approximation to > "random" is to use the system clock as a seed as this is different with each > execution of the program. > > void crearArchivo(char* nombreArchivo, int n) > { > ofstream fSalida; > fSalida.open(nombreArchivo); > srand(time(NULL)); // Set a random seed > if(fSalida.is_open()) > { > for(int i=0;i<n;i++) > { > char c=(char)(rand() % 94 + 32); //Only generate numbers > in range 32 to 126 > fSalida<<c; > } > } > else > { > cout<<"el arhivo no se pudo crear"; > } > fSalida.close(); > } > > int main(int argc, char *argv[]) > { > crearArchivo("nuevo.txt",1000) ; > system("PAUSE"); > return EXIT_SUCCESS; > } > > > Cheers, > > Matt > > > From: Jorge Guevara [mailto:jor...@gm...] > > Sent: 16 June 2009 21:25 > > To: dev...@li... > > Subject: [Dev-C++] chinese characters > > > I need to generate random characters for experiment with string matching > algorithms , n=size of text > > nombreArchivo=filename, when n is small the code below generate ascii > characters, but when n is greater, (example n > > =7000000), the code below generate chinesse characters, why? > > > sorry for my english :) > > > > void crearArchivo(char* nombreArchivo, int n) > > { > > ofstream fSalida; > > fSalida.open(nombreArchivo); > > if(fSalida.is_open()) > > { > > for(int i=0;i<n;i++) > > { char c=(char)rand(); > > fSalida<<c; > > } > > } > > else > > { > > cout<<"el arhivo no se pudo crear"; > > } > > > fSalida.close(); > > } > > > > int main() > > { > > crearArchivo("nuevo.txt",1000) ; > > system("PAUSE"); > > return EXIT_SUCCESS; > > } > > > > -- > > Jorge > This email was sent from an email account of BMW (UK) Manufacturing > Limited. BMW (UK) Manufacturing Limited is a company registered in England > and Wales with company number 3950868; and has its offices at Ellesfield > Avenue, Bracknell, Berkshire, RG12 8TA; registered in England and Wales. The > contents of this e-mail are confidential and may be privileged and subject > to internal monitoring. If you are not the addressee indicated in this > message, you may not copy, forward, disclose, deliver or otherwise use it or > any part of it in any form whatsoever. You should then kindly notify the > sender by replying to this message and destroy it thereafter. Opinions, > conclusions and other information in this message that do not relate to the > official business of any BMW Group Company shall be understood as neither > given nor endorsed by them. > -- Jorge http://jorge.sistemasyservidores.com Escuela de Informática - Universidad Nacional de Trujillo |