Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-29-2009, 04:42 PM
|
#1
|
|
LQ Newbie
Registered: Nov 2009
Location: México
Posts: 9
Rep:
|
Why i get a weird symbol at the end of a string?
hey
im trying to generate random strings of a given length with the alphabet that the user inputs from the command line.
Code:
char randchar(const char *alphabet){
int val;
float mod;
mod =(float) strlen(alphabet);
val = (int)(mod * rand() / (RAND_MAX + 1.0));
return alphabet[val];
}
this function returns the char in the random index generated. the man-pages of rand gives this way of generating a random number within a range, in my case the range is the length of the alphabet.
Code:
char *randword(const char *alphabet, const int wlength){
char *word;
int i = 0;
word = (char *)malloc(sizeof(char) * wlength);
if(!word){
printf("!mem\n");
exit(1);
}
while(i < wlength)
word[i++] = randchar(alphabet);
return word;
}
so this function calls the randchar function and builds a word with length wlength. is made like this cus i wanted to build a big file (~900 mb) with a long string, like a dna string, and i have 1g ram.
Code:
for(i=1; i < 151; i++){
pattern = randword(argv[1], i);
printf("pattern[%d]: %s\n", i, pattern);
free(pattern);
}
this is from the main function.
anywaaays, when i try to generate random words with length 1, 2, 3, 4, ... 150, i get some strings with funy symbols at the end.
here is what i get from runing the program
Code:
$ ./algoritmos lkjhgfdsa
pattern[1]: g
pattern[2]: ff
pattern[3]: kjf
pattern[4]: fhhg
pattern[5]: gsfks
pattern[6]: dghggj
pattern[7]: gajfgka
pattern[8]: jkhsslal
pattern[9]: dfhklaadl
pattern[10]: dggkllhgad
pattern[11]: lgsaslhfsgf
pattern[12]: skkjjjjjljag
pattern[13]: skgsfasjlhklk
pattern[14]: kgdlahakgkgdgd
pattern[15]: dddjfsdggdhdsda
pattern[16]: aalhdlhlafgkljds
pattern[17]: lhgjlhlgsdsfgfghg
pattern[18]: fskdkkdddasaffalkj
pattern[19]: lgjghahagagahlsgdad
pattern[20]: gdhhfjajjlhflasfjsak
pattern[21]: hadjjdlsglgasahhkhfgh
pattern[22]: llhaalkdahlalhkshlhgfh
pattern[23]: jgdddlhjhhjshjsgasaadah
pattern[24]: asdlkjfgflkjsjdldlhghfha
pattern[25]: gjsgassadfasaghgffdhshhfg
pattern[26]: skahgaadsgddhfhlgjalfhfjka
pattern[27]: lfhdkkslfjlglslsfhhahajjaad
pattern[28]: gksgjhaagkdfdlfjkgjalfhlljhh�
... so on
and i cant use those strings =/
any ideas?
|
|
|
|
11-29-2009, 05:40 PM
|
#2
|
|
Senior Member
Registered: Jan 2009
Location: vijayawada, India
Distribution: openSUSE 11.2, Ubuntu 9.0.4
Posts: 1,155
Rep:
|
In the "for loop" you had given i=1 to 151 ......... see that.
ASCII charcter set consists of 128 charcters defined as 0 to 127
in those characters, 0-31,127 are the special symbols. which are like smiley symbols,shapes ..etc.
32-126 are charcters which you can see on the keyboard.
if you ask a charcterset no beyond 127, then it agian starts from 0 ..
i.e. 128 =0
129 =1
130 =2
131 =3
.
.
.
.
.
151 =21
make a change in your for loop to stop those symbols.
|
|
|
|
11-29-2009, 06:22 PM
|
#3
|
|
LQ Newbie
Registered: Nov 2009
Location: México
Posts: 9
Original Poster
Rep:
|
hi thx for taking the time to answer
but i think thats not the problem =/
see, the loop is to make the word that long, it doesnt have anything to do with the ASCII code.
when the loop goes to 150 means: build a random word, with this alphabet, and make it 150 characters long
so it gets a random char from the alphabet and then appends it to a string until it has 150 chars, but this chars are all from the alphabet not randomized ASCII code.
|
|
|
|
11-29-2009, 07:15 PM
|
#4
|
|
Senior Member
Registered: Jan 2009
Location: vijayawada, India
Distribution: openSUSE 11.2, Ubuntu 9.0.4
Posts: 1,155
Rep:
|
ok.
See the module "randchar"
Code:
char randchar(const char *alphabet){
int val;
float mod;
mod =(float) strlen(alphabet);
val = (int)(mod * rand() / (RAND_MAX + 1.0));
return alphabet[val];
}
you said that range is length of alphabet. But you are adding 1 to it.
i.e.
if your alphabet is 9 char length. and if you want to generate a word of length 10. according to your code
the values of variable "val" will be ..{0,1,2,3,4,5,6,7,8,9,10}.
but the values of variable "val" must be ..{0,1,2,3,4,5,6,7,8,9}.
|
|
|
2 members found this post helpful.
|
11-29-2009, 07:26 PM
|
#5
|
|
Member
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 292
Rep:
|
You are not zero-terminating your string.
And if you do, your malloc() will be one byte short.
=> malloc 1 more byte, zero terminate your string.
|
|
|
1 members found this post helpful.
|
11-30-2009, 03:57 PM
|
#6
|
|
LQ Newbie
Registered: Nov 2009
Location: México
Posts: 9
Original Poster
Rep:
|
both of you were right 
it works now
Code:
char randchar(const char *alphabet){
int val;
float mod;
mod =(float) strlen(alphabet);
val = (int)(mod * rand() / RAND_MAX);
return alphabet[val];
}
this is how it looks now and it works all good
Code:
char *randword(const char *alphabet, const int wlength){
char *word;
int i = 0;
word = (char *)malloc(sizeof(char) * wlength+1);
if(!word){
printf("!mem\n");
exit(1);
}
while(i < wlength){
word[i++] = randchar(alphabet);
word[i] = '\0';
}
return word;
}
and this is with the zero char at the end
thx for your help
Last edited by gerardorn; 11-30-2009 at 03:58 PM.
|
|
|
|
11-30-2009, 05:46 PM
|
#7
|
|
Member
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 292
Rep:
|
[QUOTE=gerardorn;3774852]
Code:
...
mod =(float) strlen(alphabet);
val = (int)(mod * rand() / RAND_MAX);
return alphabet[val];
Hmm. If rand() should return RAND_MAX, val will be equal to strlen(aphabet).
That means you would read past the end of the string. You would return the terminating zero.
|
|
|
1 members found this post helpful.
|
12-01-2009, 02:08 AM
|
#8
|
|
LQ Newbie
Registered: Nov 2009
Location: México
Posts: 9
Original Poster
Rep:
|
ur right, i did this
Code:
val = (int)(mod * RAND_MAX / RAND_MAX);
and its an endless loop =/, should i use a do...while(val == mod)? or what do you think?
|
|
|
|
12-01-2009, 05:00 AM
|
#9
|
|
Member
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 292
Rep:
|
Quote:
Originally Posted by gerardorn
and its an endless loop =/, should i use a do...while(val == mod)? or what do you think?
|
I'd say the random function was ok as it was (with the '+ 1.0').
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:00 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|