How do I set a random number of random lowercase characters to a Struct member using memset in C












0















I am forced to use memset and drand48() to set a random number (2 - 7) of random characters that are lower case letters ('a' to 'z'). My code returns non ASCII characters and I am not sure why.



struct Record {
int seqnum;
float threat;
unsigned int addrs[2];
unsigned short int ports[2];
char dns_name[NUMLTRS];
};


My code is in a for loop:



memset(rec_ptr[i].dns_name, (char)((122 * drand48()) + 97), 
((sizeof(char) * 7) * drand48()) + (sizeof(char) * 2));









share|improve this question


















  • 2





    ASCII's range is only 0 - 127. Unless drand48() returns 0 in (char)((122 * drand48()) + 97), it won't fit.

    – Fiddling Bits
    Nov 21 '18 at 19:20








  • 1





    Why are you forced to use memset? Doing dns_name[i] = ... seems more logical if you have to bang out multiple characters.

    – tadman
    Nov 21 '18 at 19:23






  • 1





    sizeof char is 1 per definition. Always. And 1 is neutral for multiplication, so drop it. lrand48() would be better fitting since you want integers.

    – Swordfish
    Nov 21 '18 at 19:24








  • 2





    . o O ( everytime one uses magic numbers, a kitty dies :( :( )

    – Swordfish
    Nov 21 '18 at 19:40








  • 1





    The problem with memset is that it's a very good way of setting multiple bytes to the same value, but a very poor way of setting many bytes to different values. In this case I think it would be far simpler to use one array assignment for each letter.

    – Tim Randall
    Nov 21 '18 at 19:59
















0















I am forced to use memset and drand48() to set a random number (2 - 7) of random characters that are lower case letters ('a' to 'z'). My code returns non ASCII characters and I am not sure why.



struct Record {
int seqnum;
float threat;
unsigned int addrs[2];
unsigned short int ports[2];
char dns_name[NUMLTRS];
};


My code is in a for loop:



memset(rec_ptr[i].dns_name, (char)((122 * drand48()) + 97), 
((sizeof(char) * 7) * drand48()) + (sizeof(char) * 2));









share|improve this question


















  • 2





    ASCII's range is only 0 - 127. Unless drand48() returns 0 in (char)((122 * drand48()) + 97), it won't fit.

    – Fiddling Bits
    Nov 21 '18 at 19:20








  • 1





    Why are you forced to use memset? Doing dns_name[i] = ... seems more logical if you have to bang out multiple characters.

    – tadman
    Nov 21 '18 at 19:23






  • 1





    sizeof char is 1 per definition. Always. And 1 is neutral for multiplication, so drop it. lrand48() would be better fitting since you want integers.

    – Swordfish
    Nov 21 '18 at 19:24








  • 2





    . o O ( everytime one uses magic numbers, a kitty dies :( :( )

    – Swordfish
    Nov 21 '18 at 19:40








  • 1





    The problem with memset is that it's a very good way of setting multiple bytes to the same value, but a very poor way of setting many bytes to different values. In this case I think it would be far simpler to use one array assignment for each letter.

    – Tim Randall
    Nov 21 '18 at 19:59














0












0








0








I am forced to use memset and drand48() to set a random number (2 - 7) of random characters that are lower case letters ('a' to 'z'). My code returns non ASCII characters and I am not sure why.



struct Record {
int seqnum;
float threat;
unsigned int addrs[2];
unsigned short int ports[2];
char dns_name[NUMLTRS];
};


My code is in a for loop:



memset(rec_ptr[i].dns_name, (char)((122 * drand48()) + 97), 
((sizeof(char) * 7) * drand48()) + (sizeof(char) * 2));









share|improve this question














I am forced to use memset and drand48() to set a random number (2 - 7) of random characters that are lower case letters ('a' to 'z'). My code returns non ASCII characters and I am not sure why.



struct Record {
int seqnum;
float threat;
unsigned int addrs[2];
unsigned short int ports[2];
char dns_name[NUMLTRS];
};


My code is in a for loop:



memset(rec_ptr[i].dns_name, (char)((122 * drand48()) + 97), 
((sizeof(char) * 7) * drand48()) + (sizeof(char) * 2));






c random memset






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 19:19









jch3jch3

11




11








  • 2





    ASCII's range is only 0 - 127. Unless drand48() returns 0 in (char)((122 * drand48()) + 97), it won't fit.

    – Fiddling Bits
    Nov 21 '18 at 19:20








  • 1





    Why are you forced to use memset? Doing dns_name[i] = ... seems more logical if you have to bang out multiple characters.

    – tadman
    Nov 21 '18 at 19:23






  • 1





    sizeof char is 1 per definition. Always. And 1 is neutral for multiplication, so drop it. lrand48() would be better fitting since you want integers.

    – Swordfish
    Nov 21 '18 at 19:24








  • 2





    . o O ( everytime one uses magic numbers, a kitty dies :( :( )

    – Swordfish
    Nov 21 '18 at 19:40








  • 1





    The problem with memset is that it's a very good way of setting multiple bytes to the same value, but a very poor way of setting many bytes to different values. In this case I think it would be far simpler to use one array assignment for each letter.

    – Tim Randall
    Nov 21 '18 at 19:59














  • 2





    ASCII's range is only 0 - 127. Unless drand48() returns 0 in (char)((122 * drand48()) + 97), it won't fit.

    – Fiddling Bits
    Nov 21 '18 at 19:20








  • 1





    Why are you forced to use memset? Doing dns_name[i] = ... seems more logical if you have to bang out multiple characters.

    – tadman
    Nov 21 '18 at 19:23






  • 1





    sizeof char is 1 per definition. Always. And 1 is neutral for multiplication, so drop it. lrand48() would be better fitting since you want integers.

    – Swordfish
    Nov 21 '18 at 19:24








  • 2





    . o O ( everytime one uses magic numbers, a kitty dies :( :( )

    – Swordfish
    Nov 21 '18 at 19:40








  • 1





    The problem with memset is that it's a very good way of setting multiple bytes to the same value, but a very poor way of setting many bytes to different values. In this case I think it would be far simpler to use one array assignment for each letter.

    – Tim Randall
    Nov 21 '18 at 19:59








2




2





ASCII's range is only 0 - 127. Unless drand48() returns 0 in (char)((122 * drand48()) + 97), it won't fit.

– Fiddling Bits
Nov 21 '18 at 19:20







ASCII's range is only 0 - 127. Unless drand48() returns 0 in (char)((122 * drand48()) + 97), it won't fit.

– Fiddling Bits
Nov 21 '18 at 19:20






1




1





Why are you forced to use memset? Doing dns_name[i] = ... seems more logical if you have to bang out multiple characters.

– tadman
Nov 21 '18 at 19:23





Why are you forced to use memset? Doing dns_name[i] = ... seems more logical if you have to bang out multiple characters.

– tadman
Nov 21 '18 at 19:23




1




1





sizeof char is 1 per definition. Always. And 1 is neutral for multiplication, so drop it. lrand48() would be better fitting since you want integers.

– Swordfish
Nov 21 '18 at 19:24







sizeof char is 1 per definition. Always. And 1 is neutral for multiplication, so drop it. lrand48() would be better fitting since you want integers.

– Swordfish
Nov 21 '18 at 19:24






2




2





. o O ( everytime one uses magic numbers, a kitty dies :( :( )

– Swordfish
Nov 21 '18 at 19:40







. o O ( everytime one uses magic numbers, a kitty dies :( :( )

– Swordfish
Nov 21 '18 at 19:40






1




1





The problem with memset is that it's a very good way of setting multiple bytes to the same value, but a very poor way of setting many bytes to different values. In this case I think it would be far simpler to use one array assignment for each letter.

– Tim Randall
Nov 21 '18 at 19:59





The problem with memset is that it's a very good way of setting multiple bytes to the same value, but a very poor way of setting many bytes to different values. In this case I think it would be far simpler to use one array assignment for each letter.

– Tim Randall
Nov 21 '18 at 19:59












1 Answer
1






active

oldest

votes


















1















My code returns non ASCII characters and I am not sure why.




Wrong scale used to generate lower case letters.



(122 * drand48()) + 97 converted to an integer type can readily make 122 different values. [97...218]. This is outside the ASCII range of [0...127].






How do I set a random number of random lowercase character ...




drand48() provides a random value [0...1.0). Scale by 26 and truncate to get 26 different indexes.



int index = (int) (drand48()*26);  // 0...25


Pedantic code would be concerned about the few random values that may round the product to 26.0



if (index >= 26) index = 26 - 1;
int az = index + 'a';
// or look up in a table if non-ASCII encoding might be used
// 12345678901234567890123456
int az = "abcdefghijklmnopqrstuvwxyz"[index];


Selecting a random length would use the same thing, but with NUMLTRS instead of 26.



int length = (int) (drand48()*NUMLTRS); 
if (index >= NUMLTRS) index = NUMLTRS -1;





... to a Struct member using memset in C




It is unclear if dns_name should be all the same, or generally different letters.



struct Record foo;
if (all_same) [
memset(foo.dns_name, az, length);
} else {
for (int i = 0; i < length; i++) {
int index = (int) (drand48()*26); // 0...25
if (index >= 26) index = 26 -1;
int az = index + 'a';
foo.dns_name[i] = az; // Does not make sense to use memset() here
}
}




Lastly, if dns_name is meant to be a string for ease of later use, declare with a +1 size



dns_name[NUMLTRS + 1];

// above code

foo.dns_name[NUMLTRS] = ''; // append null character
printf("dna_name <%s>n", foo.dns_name);





share|improve this answer


























  • Can (int)(0.9999...) really get rounded up to 1? "When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero)" (6.3.1.4 of ISO/IEC 9899:1999)

    – usr2564301
    Nov 21 '18 at 23:04






  • 1





    @usr2564301 The issue is not about (int)(0.9999...), but could 0.9999... * 26 under any rounding mode and any FLT_EVAL_MODE result in 26.0? True, mathematically 0.9999... * 26 is always less than 26, but this is FP math. A deep analysis may show 26.0 is not possible, but not having done that, a check is prudent.

    – chux
    Nov 21 '18 at 23:08














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419153%2fhow-do-i-set-a-random-number-of-random-lowercase-characters-to-a-struct-member-u%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















My code returns non ASCII characters and I am not sure why.




Wrong scale used to generate lower case letters.



(122 * drand48()) + 97 converted to an integer type can readily make 122 different values. [97...218]. This is outside the ASCII range of [0...127].






How do I set a random number of random lowercase character ...




drand48() provides a random value [0...1.0). Scale by 26 and truncate to get 26 different indexes.



int index = (int) (drand48()*26);  // 0...25


Pedantic code would be concerned about the few random values that may round the product to 26.0



if (index >= 26) index = 26 - 1;
int az = index + 'a';
// or look up in a table if non-ASCII encoding might be used
// 12345678901234567890123456
int az = "abcdefghijklmnopqrstuvwxyz"[index];


Selecting a random length would use the same thing, but with NUMLTRS instead of 26.



int length = (int) (drand48()*NUMLTRS); 
if (index >= NUMLTRS) index = NUMLTRS -1;





... to a Struct member using memset in C




It is unclear if dns_name should be all the same, or generally different letters.



struct Record foo;
if (all_same) [
memset(foo.dns_name, az, length);
} else {
for (int i = 0; i < length; i++) {
int index = (int) (drand48()*26); // 0...25
if (index >= 26) index = 26 -1;
int az = index + 'a';
foo.dns_name[i] = az; // Does not make sense to use memset() here
}
}




Lastly, if dns_name is meant to be a string for ease of later use, declare with a +1 size



dns_name[NUMLTRS + 1];

// above code

foo.dns_name[NUMLTRS] = ''; // append null character
printf("dna_name <%s>n", foo.dns_name);





share|improve this answer


























  • Can (int)(0.9999...) really get rounded up to 1? "When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero)" (6.3.1.4 of ISO/IEC 9899:1999)

    – usr2564301
    Nov 21 '18 at 23:04






  • 1





    @usr2564301 The issue is not about (int)(0.9999...), but could 0.9999... * 26 under any rounding mode and any FLT_EVAL_MODE result in 26.0? True, mathematically 0.9999... * 26 is always less than 26, but this is FP math. A deep analysis may show 26.0 is not possible, but not having done that, a check is prudent.

    – chux
    Nov 21 '18 at 23:08


















1















My code returns non ASCII characters and I am not sure why.




Wrong scale used to generate lower case letters.



(122 * drand48()) + 97 converted to an integer type can readily make 122 different values. [97...218]. This is outside the ASCII range of [0...127].






How do I set a random number of random lowercase character ...




drand48() provides a random value [0...1.0). Scale by 26 and truncate to get 26 different indexes.



int index = (int) (drand48()*26);  // 0...25


Pedantic code would be concerned about the few random values that may round the product to 26.0



if (index >= 26) index = 26 - 1;
int az = index + 'a';
// or look up in a table if non-ASCII encoding might be used
// 12345678901234567890123456
int az = "abcdefghijklmnopqrstuvwxyz"[index];


Selecting a random length would use the same thing, but with NUMLTRS instead of 26.



int length = (int) (drand48()*NUMLTRS); 
if (index >= NUMLTRS) index = NUMLTRS -1;





... to a Struct member using memset in C




It is unclear if dns_name should be all the same, or generally different letters.



struct Record foo;
if (all_same) [
memset(foo.dns_name, az, length);
} else {
for (int i = 0; i < length; i++) {
int index = (int) (drand48()*26); // 0...25
if (index >= 26) index = 26 -1;
int az = index + 'a';
foo.dns_name[i] = az; // Does not make sense to use memset() here
}
}




Lastly, if dns_name is meant to be a string for ease of later use, declare with a +1 size



dns_name[NUMLTRS + 1];

// above code

foo.dns_name[NUMLTRS] = ''; // append null character
printf("dna_name <%s>n", foo.dns_name);





share|improve this answer


























  • Can (int)(0.9999...) really get rounded up to 1? "When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero)" (6.3.1.4 of ISO/IEC 9899:1999)

    – usr2564301
    Nov 21 '18 at 23:04






  • 1





    @usr2564301 The issue is not about (int)(0.9999...), but could 0.9999... * 26 under any rounding mode and any FLT_EVAL_MODE result in 26.0? True, mathematically 0.9999... * 26 is always less than 26, but this is FP math. A deep analysis may show 26.0 is not possible, but not having done that, a check is prudent.

    – chux
    Nov 21 '18 at 23:08
















1












1








1








My code returns non ASCII characters and I am not sure why.




Wrong scale used to generate lower case letters.



(122 * drand48()) + 97 converted to an integer type can readily make 122 different values. [97...218]. This is outside the ASCII range of [0...127].






How do I set a random number of random lowercase character ...




drand48() provides a random value [0...1.0). Scale by 26 and truncate to get 26 different indexes.



int index = (int) (drand48()*26);  // 0...25


Pedantic code would be concerned about the few random values that may round the product to 26.0



if (index >= 26) index = 26 - 1;
int az = index + 'a';
// or look up in a table if non-ASCII encoding might be used
// 12345678901234567890123456
int az = "abcdefghijklmnopqrstuvwxyz"[index];


Selecting a random length would use the same thing, but with NUMLTRS instead of 26.



int length = (int) (drand48()*NUMLTRS); 
if (index >= NUMLTRS) index = NUMLTRS -1;





... to a Struct member using memset in C




It is unclear if dns_name should be all the same, or generally different letters.



struct Record foo;
if (all_same) [
memset(foo.dns_name, az, length);
} else {
for (int i = 0; i < length; i++) {
int index = (int) (drand48()*26); // 0...25
if (index >= 26) index = 26 -1;
int az = index + 'a';
foo.dns_name[i] = az; // Does not make sense to use memset() here
}
}




Lastly, if dns_name is meant to be a string for ease of later use, declare with a +1 size



dns_name[NUMLTRS + 1];

// above code

foo.dns_name[NUMLTRS] = ''; // append null character
printf("dna_name <%s>n", foo.dns_name);





share|improve this answer
















My code returns non ASCII characters and I am not sure why.




Wrong scale used to generate lower case letters.



(122 * drand48()) + 97 converted to an integer type can readily make 122 different values. [97...218]. This is outside the ASCII range of [0...127].






How do I set a random number of random lowercase character ...




drand48() provides a random value [0...1.0). Scale by 26 and truncate to get 26 different indexes.



int index = (int) (drand48()*26);  // 0...25


Pedantic code would be concerned about the few random values that may round the product to 26.0



if (index >= 26) index = 26 - 1;
int az = index + 'a';
// or look up in a table if non-ASCII encoding might be used
// 12345678901234567890123456
int az = "abcdefghijklmnopqrstuvwxyz"[index];


Selecting a random length would use the same thing, but with NUMLTRS instead of 26.



int length = (int) (drand48()*NUMLTRS); 
if (index >= NUMLTRS) index = NUMLTRS -1;





... to a Struct member using memset in C




It is unclear if dns_name should be all the same, or generally different letters.



struct Record foo;
if (all_same) [
memset(foo.dns_name, az, length);
} else {
for (int i = 0; i < length; i++) {
int index = (int) (drand48()*26); // 0...25
if (index >= 26) index = 26 -1;
int az = index + 'a';
foo.dns_name[i] = az; // Does not make sense to use memset() here
}
}




Lastly, if dns_name is meant to be a string for ease of later use, declare with a +1 size



dns_name[NUMLTRS + 1];

// above code

foo.dns_name[NUMLTRS] = ''; // append null character
printf("dna_name <%s>n", foo.dns_name);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 23:20

























answered Nov 21 '18 at 22:57









chuxchux

84.8k874157




84.8k874157













  • Can (int)(0.9999...) really get rounded up to 1? "When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero)" (6.3.1.4 of ISO/IEC 9899:1999)

    – usr2564301
    Nov 21 '18 at 23:04






  • 1





    @usr2564301 The issue is not about (int)(0.9999...), but could 0.9999... * 26 under any rounding mode and any FLT_EVAL_MODE result in 26.0? True, mathematically 0.9999... * 26 is always less than 26, but this is FP math. A deep analysis may show 26.0 is not possible, but not having done that, a check is prudent.

    – chux
    Nov 21 '18 at 23:08





















  • Can (int)(0.9999...) really get rounded up to 1? "When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero)" (6.3.1.4 of ISO/IEC 9899:1999)

    – usr2564301
    Nov 21 '18 at 23:04






  • 1





    @usr2564301 The issue is not about (int)(0.9999...), but could 0.9999... * 26 under any rounding mode and any FLT_EVAL_MODE result in 26.0? True, mathematically 0.9999... * 26 is always less than 26, but this is FP math. A deep analysis may show 26.0 is not possible, but not having done that, a check is prudent.

    – chux
    Nov 21 '18 at 23:08



















Can (int)(0.9999...) really get rounded up to 1? "When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero)" (6.3.1.4 of ISO/IEC 9899:1999)

– usr2564301
Nov 21 '18 at 23:04





Can (int)(0.9999...) really get rounded up to 1? "When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero)" (6.3.1.4 of ISO/IEC 9899:1999)

– usr2564301
Nov 21 '18 at 23:04




1




1





@usr2564301 The issue is not about (int)(0.9999...), but could 0.9999... * 26 under any rounding mode and any FLT_EVAL_MODE result in 26.0? True, mathematically 0.9999... * 26 is always less than 26, but this is FP math. A deep analysis may show 26.0 is not possible, but not having done that, a check is prudent.

– chux
Nov 21 '18 at 23:08







@usr2564301 The issue is not about (int)(0.9999...), but could 0.9999... * 26 under any rounding mode and any FLT_EVAL_MODE result in 26.0? True, mathematically 0.9999... * 26 is always less than 26, but this is FP math. A deep analysis may show 26.0 is not possible, but not having done that, a check is prudent.

– chux
Nov 21 '18 at 23:08






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419153%2fhow-do-i-set-a-random-number-of-random-lowercase-characters-to-a-struct-member-u%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)