How do I set a random number of random lowercase characters to a Struct member using memset in C
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
|
show 7 more comments
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
2
ASCII's range is only0
-127
. Unlessdrand48()
return
s0
in(char)((122 * drand48()) + 97)
, it won't fit.
– Fiddling Bits
Nov 21 '18 at 19:20
1
Why are you forced to usememset
? Doingdns_name[i] = ...
seems more logical if you have to bang out multiple characters.
– tadman
Nov 21 '18 at 19:23
1
sizeof char
is1
per definition. Always. And1
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 withmemset
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
|
show 7 more comments
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
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
c random memset
asked Nov 21 '18 at 19:19
jch3jch3
11
11
2
ASCII's range is only0
-127
. Unlessdrand48()
return
s0
in(char)((122 * drand48()) + 97)
, it won't fit.
– Fiddling Bits
Nov 21 '18 at 19:20
1
Why are you forced to usememset
? Doingdns_name[i] = ...
seems more logical if you have to bang out multiple characters.
– tadman
Nov 21 '18 at 19:23
1
sizeof char
is1
per definition. Always. And1
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 withmemset
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
|
show 7 more comments
2
ASCII's range is only0
-127
. Unlessdrand48()
return
s0
in(char)((122 * drand48()) + 97)
, it won't fit.
– Fiddling Bits
Nov 21 '18 at 19:20
1
Why are you forced to usememset
? Doingdns_name[i] = ...
seems more logical if you have to bang out multiple characters.
– tadman
Nov 21 '18 at 19:23
1
sizeof char
is1
per definition. Always. And1
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 withmemset
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()
return
s 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()
return
s 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
|
show 7 more comments
1 Answer
1
active
oldest
votes
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);
Can(int)(0.9999...)
really get rounded up to1
? "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 could0.9999... * 26
under any rounding mode and anyFLT_EVAL_MODE
result in 26.0? True, mathematically0.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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
Can(int)(0.9999...)
really get rounded up to1
? "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 could0.9999... * 26
under any rounding mode and anyFLT_EVAL_MODE
result in 26.0? True, mathematically0.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
add a comment |
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);
Can(int)(0.9999...)
really get rounded up to1
? "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 could0.9999... * 26
under any rounding mode and anyFLT_EVAL_MODE
result in 26.0? True, mathematically0.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
add a comment |
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);
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);
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 to1
? "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 could0.9999... * 26
under any rounding mode and anyFLT_EVAL_MODE
result in 26.0? True, mathematically0.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
add a comment |
Can(int)(0.9999...)
really get rounded up to1
? "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 could0.9999... * 26
under any rounding mode and anyFLT_EVAL_MODE
result in 26.0? True, mathematically0.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
ASCII's range is only
0
-127
. Unlessdrand48()
return
s0
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
? Doingdns_name[i] = ...
seems more logical if you have to bang out multiple characters.– tadman
Nov 21 '18 at 19:23
1
sizeof char
is1
per definition. Always. And1
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