How to generate an 8-bit random number in C>
int main(){
uint8_t *wdata = NULL;
wdata = calloc(5, sizeof(uint8_t));
for (int j =0;j<5;j++){
wdata[j] = rand();
}
}
The rand()
function generates 16 bits of data. How do I generate 8 bits of random values? Do I need to use a custom function for the same?
c random types
|
show 1 more comment
int main(){
uint8_t *wdata = NULL;
wdata = calloc(5, sizeof(uint8_t));
for (int j =0;j<5;j++){
wdata[j] = rand();
}
}
The rand()
function generates 16 bits of data. How do I generate 8 bits of random values? Do I need to use a custom function for the same?
c random types
1
So rand returns 16bits, just use 8 of it and discard the rest.
– Kamil Cuk
Nov 19 '18 at 16:10
3
What you've got will assign the least significant 8 bits of the 16-bit value that you say is returned byrand()
. Many versions ofrand()
will return 32-bit values, but that's a separate discussion. The only problem with using the least significant 8 bits is that they may not be as random as you'd like — they might fail various tests of randomness. How much that matters depends on what you're going to do next. Most likely, you can simply use the code shown. Failing that, you probably need a better pseudo-random number generator (PRNG) thanrand()
.
– Jonathan Leffler
Nov 19 '18 at 16:10
1
@JonathanLeffler If you discard some of the random value by a power of 2, it'll still be an even distribution of randomness, won't it? e.g.rand() % 16
,rand() % 32
,rand() % 64
, etc.
– Fiddling Bits
Nov 19 '18 at 16:19
1
See stackoverflow.com/questions/11418113/… and especially links from it, such as to the paper by Marsaglia (sp?).
– Jonathan Leffler
Nov 19 '18 at 16:48
3
@fiddling: if rand is implemented with a.linear congruential generator, then the period of rand%2i is 2i, which is not very random for smalli
.
– rici
Nov 19 '18 at 18:32
|
show 1 more comment
int main(){
uint8_t *wdata = NULL;
wdata = calloc(5, sizeof(uint8_t));
for (int j =0;j<5;j++){
wdata[j] = rand();
}
}
The rand()
function generates 16 bits of data. How do I generate 8 bits of random values? Do I need to use a custom function for the same?
c random types
int main(){
uint8_t *wdata = NULL;
wdata = calloc(5, sizeof(uint8_t));
for (int j =0;j<5;j++){
wdata[j] = rand();
}
}
The rand()
function generates 16 bits of data. How do I generate 8 bits of random values? Do I need to use a custom function for the same?
c random types
c random types
edited Nov 19 '18 at 16:07


Jonathan Leffler
561k896661018
561k896661018
asked Nov 19 '18 at 16:04
ShivaniSarin
13
13
1
So rand returns 16bits, just use 8 of it and discard the rest.
– Kamil Cuk
Nov 19 '18 at 16:10
3
What you've got will assign the least significant 8 bits of the 16-bit value that you say is returned byrand()
. Many versions ofrand()
will return 32-bit values, but that's a separate discussion. The only problem with using the least significant 8 bits is that they may not be as random as you'd like — they might fail various tests of randomness. How much that matters depends on what you're going to do next. Most likely, you can simply use the code shown. Failing that, you probably need a better pseudo-random number generator (PRNG) thanrand()
.
– Jonathan Leffler
Nov 19 '18 at 16:10
1
@JonathanLeffler If you discard some of the random value by a power of 2, it'll still be an even distribution of randomness, won't it? e.g.rand() % 16
,rand() % 32
,rand() % 64
, etc.
– Fiddling Bits
Nov 19 '18 at 16:19
1
See stackoverflow.com/questions/11418113/… and especially links from it, such as to the paper by Marsaglia (sp?).
– Jonathan Leffler
Nov 19 '18 at 16:48
3
@fiddling: if rand is implemented with a.linear congruential generator, then the period of rand%2i is 2i, which is not very random for smalli
.
– rici
Nov 19 '18 at 18:32
|
show 1 more comment
1
So rand returns 16bits, just use 8 of it and discard the rest.
– Kamil Cuk
Nov 19 '18 at 16:10
3
What you've got will assign the least significant 8 bits of the 16-bit value that you say is returned byrand()
. Many versions ofrand()
will return 32-bit values, but that's a separate discussion. The only problem with using the least significant 8 bits is that they may not be as random as you'd like — they might fail various tests of randomness. How much that matters depends on what you're going to do next. Most likely, you can simply use the code shown. Failing that, you probably need a better pseudo-random number generator (PRNG) thanrand()
.
– Jonathan Leffler
Nov 19 '18 at 16:10
1
@JonathanLeffler If you discard some of the random value by a power of 2, it'll still be an even distribution of randomness, won't it? e.g.rand() % 16
,rand() % 32
,rand() % 64
, etc.
– Fiddling Bits
Nov 19 '18 at 16:19
1
See stackoverflow.com/questions/11418113/… and especially links from it, such as to the paper by Marsaglia (sp?).
– Jonathan Leffler
Nov 19 '18 at 16:48
3
@fiddling: if rand is implemented with a.linear congruential generator, then the period of rand%2i is 2i, which is not very random for smalli
.
– rici
Nov 19 '18 at 18:32
1
1
So rand returns 16bits, just use 8 of it and discard the rest.
– Kamil Cuk
Nov 19 '18 at 16:10
So rand returns 16bits, just use 8 of it and discard the rest.
– Kamil Cuk
Nov 19 '18 at 16:10
3
3
What you've got will assign the least significant 8 bits of the 16-bit value that you say is returned by
rand()
. Many versions of rand()
will return 32-bit values, but that's a separate discussion. The only problem with using the least significant 8 bits is that they may not be as random as you'd like — they might fail various tests of randomness. How much that matters depends on what you're going to do next. Most likely, you can simply use the code shown. Failing that, you probably need a better pseudo-random number generator (PRNG) than rand()
.– Jonathan Leffler
Nov 19 '18 at 16:10
What you've got will assign the least significant 8 bits of the 16-bit value that you say is returned by
rand()
. Many versions of rand()
will return 32-bit values, but that's a separate discussion. The only problem with using the least significant 8 bits is that they may not be as random as you'd like — they might fail various tests of randomness. How much that matters depends on what you're going to do next. Most likely, you can simply use the code shown. Failing that, you probably need a better pseudo-random number generator (PRNG) than rand()
.– Jonathan Leffler
Nov 19 '18 at 16:10
1
1
@JonathanLeffler If you discard some of the random value by a power of 2, it'll still be an even distribution of randomness, won't it? e.g.
rand() % 16
, rand() % 32
, rand() % 64
, etc.– Fiddling Bits
Nov 19 '18 at 16:19
@JonathanLeffler If you discard some of the random value by a power of 2, it'll still be an even distribution of randomness, won't it? e.g.
rand() % 16
, rand() % 32
, rand() % 64
, etc.– Fiddling Bits
Nov 19 '18 at 16:19
1
1
See stackoverflow.com/questions/11418113/… and especially links from it, such as to the paper by Marsaglia (sp?).
– Jonathan Leffler
Nov 19 '18 at 16:48
See stackoverflow.com/questions/11418113/… and especially links from it, such as to the paper by Marsaglia (sp?).
– Jonathan Leffler
Nov 19 '18 at 16:48
3
3
@fiddling: if rand is implemented with a.linear congruential generator, then the period of rand%2i is 2i, which is not very random for small
i
.– rici
Nov 19 '18 at 18:32
@fiddling: if rand is implemented with a.linear congruential generator, then the period of rand%2i is 2i, which is not very random for small
i
.– rici
Nov 19 '18 at 18:32
|
show 1 more comment
2 Answers
2
active
oldest
votes
How to generate an 8 bit random number in C
Given code such as
uint8_t *wdata = calloc( 5, sizeof( uint8_t ) );
something like
for ( int j = 0;j < 5; j++ )
{
wdata[ j ] = rand();
}
will work fine.
Integer assignment to an unsigned value truncates, which is exactly what you want. See 6.5.16.1 Simple assignment of the C Standard.
Note that RAND_MAX
is guaranteed by 7.22.2 Pseudo-random sequence generation functions, paragraph 5 to be at least 32767, which is "wider" than 8 bits, so the result is guaranteed to "fill up" an 8-bit variable.
Yes, but, c-faq.com/lib/notveryrand.html. See c-faq.com/lib/randrange.html.
– Neil Edelman
Nov 19 '18 at 19:58
1
@NeilEdelman A poor PRNG is a poor PRNG, and when the modulo value is much less than the maximum result from the PRNG the modulo operation itself won't add any measurable bias to the result.
– Andrew Henle
Nov 20 '18 at 17:38
add a comment |
Because 8 bit number can contain maximum 0-255. so if i divide rand()%256 we get 8 bit random number.
int main(){
uint8_t *wdata = NULL;
wdata = calloc(5, sizeof(uint8_t));
for (int j =0;j<5;j++){
wdata[j] = rand()%256;
}
}
for more randomness you use some prime number add or subtract difference after dividing from 256.
Since they are unsigned 8 bit numbers,rand()%256
andrand()
produce the same thing; although, it may be some benefit for readability.
– Neil Edelman
Nov 19 '18 at 19:14
rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.
– Susheel Dwivedi
Nov 19 '18 at 19:24
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%2f53378519%2fhow-to-generate-an-8-bit-random-number-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
How to generate an 8 bit random number in C
Given code such as
uint8_t *wdata = calloc( 5, sizeof( uint8_t ) );
something like
for ( int j = 0;j < 5; j++ )
{
wdata[ j ] = rand();
}
will work fine.
Integer assignment to an unsigned value truncates, which is exactly what you want. See 6.5.16.1 Simple assignment of the C Standard.
Note that RAND_MAX
is guaranteed by 7.22.2 Pseudo-random sequence generation functions, paragraph 5 to be at least 32767, which is "wider" than 8 bits, so the result is guaranteed to "fill up" an 8-bit variable.
Yes, but, c-faq.com/lib/notveryrand.html. See c-faq.com/lib/randrange.html.
– Neil Edelman
Nov 19 '18 at 19:58
1
@NeilEdelman A poor PRNG is a poor PRNG, and when the modulo value is much less than the maximum result from the PRNG the modulo operation itself won't add any measurable bias to the result.
– Andrew Henle
Nov 20 '18 at 17:38
add a comment |
How to generate an 8 bit random number in C
Given code such as
uint8_t *wdata = calloc( 5, sizeof( uint8_t ) );
something like
for ( int j = 0;j < 5; j++ )
{
wdata[ j ] = rand();
}
will work fine.
Integer assignment to an unsigned value truncates, which is exactly what you want. See 6.5.16.1 Simple assignment of the C Standard.
Note that RAND_MAX
is guaranteed by 7.22.2 Pseudo-random sequence generation functions, paragraph 5 to be at least 32767, which is "wider" than 8 bits, so the result is guaranteed to "fill up" an 8-bit variable.
Yes, but, c-faq.com/lib/notveryrand.html. See c-faq.com/lib/randrange.html.
– Neil Edelman
Nov 19 '18 at 19:58
1
@NeilEdelman A poor PRNG is a poor PRNG, and when the modulo value is much less than the maximum result from the PRNG the modulo operation itself won't add any measurable bias to the result.
– Andrew Henle
Nov 20 '18 at 17:38
add a comment |
How to generate an 8 bit random number in C
Given code such as
uint8_t *wdata = calloc( 5, sizeof( uint8_t ) );
something like
for ( int j = 0;j < 5; j++ )
{
wdata[ j ] = rand();
}
will work fine.
Integer assignment to an unsigned value truncates, which is exactly what you want. See 6.5.16.1 Simple assignment of the C Standard.
Note that RAND_MAX
is guaranteed by 7.22.2 Pseudo-random sequence generation functions, paragraph 5 to be at least 32767, which is "wider" than 8 bits, so the result is guaranteed to "fill up" an 8-bit variable.
How to generate an 8 bit random number in C
Given code such as
uint8_t *wdata = calloc( 5, sizeof( uint8_t ) );
something like
for ( int j = 0;j < 5; j++ )
{
wdata[ j ] = rand();
}
will work fine.
Integer assignment to an unsigned value truncates, which is exactly what you want. See 6.5.16.1 Simple assignment of the C Standard.
Note that RAND_MAX
is guaranteed by 7.22.2 Pseudo-random sequence generation functions, paragraph 5 to be at least 32767, which is "wider" than 8 bits, so the result is guaranteed to "fill up" an 8-bit variable.
edited Nov 19 '18 at 17:44


Jonathan Leffler
561k896661018
561k896661018
answered Nov 19 '18 at 16:10
Andrew Henle
19.5k21333
19.5k21333
Yes, but, c-faq.com/lib/notveryrand.html. See c-faq.com/lib/randrange.html.
– Neil Edelman
Nov 19 '18 at 19:58
1
@NeilEdelman A poor PRNG is a poor PRNG, and when the modulo value is much less than the maximum result from the PRNG the modulo operation itself won't add any measurable bias to the result.
– Andrew Henle
Nov 20 '18 at 17:38
add a comment |
Yes, but, c-faq.com/lib/notveryrand.html. See c-faq.com/lib/randrange.html.
– Neil Edelman
Nov 19 '18 at 19:58
1
@NeilEdelman A poor PRNG is a poor PRNG, and when the modulo value is much less than the maximum result from the PRNG the modulo operation itself won't add any measurable bias to the result.
– Andrew Henle
Nov 20 '18 at 17:38
Yes, but, c-faq.com/lib/notveryrand.html. See c-faq.com/lib/randrange.html.
– Neil Edelman
Nov 19 '18 at 19:58
Yes, but, c-faq.com/lib/notveryrand.html. See c-faq.com/lib/randrange.html.
– Neil Edelman
Nov 19 '18 at 19:58
1
1
@NeilEdelman A poor PRNG is a poor PRNG, and when the modulo value is much less than the maximum result from the PRNG the modulo operation itself won't add any measurable bias to the result.
– Andrew Henle
Nov 20 '18 at 17:38
@NeilEdelman A poor PRNG is a poor PRNG, and when the modulo value is much less than the maximum result from the PRNG the modulo operation itself won't add any measurable bias to the result.
– Andrew Henle
Nov 20 '18 at 17:38
add a comment |
Because 8 bit number can contain maximum 0-255. so if i divide rand()%256 we get 8 bit random number.
int main(){
uint8_t *wdata = NULL;
wdata = calloc(5, sizeof(uint8_t));
for (int j =0;j<5;j++){
wdata[j] = rand()%256;
}
}
for more randomness you use some prime number add or subtract difference after dividing from 256.
Since they are unsigned 8 bit numbers,rand()%256
andrand()
produce the same thing; although, it may be some benefit for readability.
– Neil Edelman
Nov 19 '18 at 19:14
rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.
– Susheel Dwivedi
Nov 19 '18 at 19:24
add a comment |
Because 8 bit number can contain maximum 0-255. so if i divide rand()%256 we get 8 bit random number.
int main(){
uint8_t *wdata = NULL;
wdata = calloc(5, sizeof(uint8_t));
for (int j =0;j<5;j++){
wdata[j] = rand()%256;
}
}
for more randomness you use some prime number add or subtract difference after dividing from 256.
Since they are unsigned 8 bit numbers,rand()%256
andrand()
produce the same thing; although, it may be some benefit for readability.
– Neil Edelman
Nov 19 '18 at 19:14
rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.
– Susheel Dwivedi
Nov 19 '18 at 19:24
add a comment |
Because 8 bit number can contain maximum 0-255. so if i divide rand()%256 we get 8 bit random number.
int main(){
uint8_t *wdata = NULL;
wdata = calloc(5, sizeof(uint8_t));
for (int j =0;j<5;j++){
wdata[j] = rand()%256;
}
}
for more randomness you use some prime number add or subtract difference after dividing from 256.
Because 8 bit number can contain maximum 0-255. so if i divide rand()%256 we get 8 bit random number.
int main(){
uint8_t *wdata = NULL;
wdata = calloc(5, sizeof(uint8_t));
for (int j =0;j<5;j++){
wdata[j] = rand()%256;
}
}
for more randomness you use some prime number add or subtract difference after dividing from 256.
answered Nov 19 '18 at 16:42


Susheel Dwivedi
554
554
Since they are unsigned 8 bit numbers,rand()%256
andrand()
produce the same thing; although, it may be some benefit for readability.
– Neil Edelman
Nov 19 '18 at 19:14
rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.
– Susheel Dwivedi
Nov 19 '18 at 19:24
add a comment |
Since they are unsigned 8 bit numbers,rand()%256
andrand()
produce the same thing; although, it may be some benefit for readability.
– Neil Edelman
Nov 19 '18 at 19:14
rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.
– Susheel Dwivedi
Nov 19 '18 at 19:24
Since they are unsigned 8 bit numbers,
rand()%256
and rand()
produce the same thing; although, it may be some benefit for readability.– Neil Edelman
Nov 19 '18 at 19:14
Since they are unsigned 8 bit numbers,
rand()%256
and rand()
produce the same thing; although, it may be some benefit for readability.– Neil Edelman
Nov 19 '18 at 19:14
rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.
– Susheel Dwivedi
Nov 19 '18 at 19:24
rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.
– Susheel Dwivedi
Nov 19 '18 at 19:24
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53378519%2fhow-to-generate-an-8-bit-random-number-in-c%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
1
So rand returns 16bits, just use 8 of it and discard the rest.
– Kamil Cuk
Nov 19 '18 at 16:10
3
What you've got will assign the least significant 8 bits of the 16-bit value that you say is returned by
rand()
. Many versions ofrand()
will return 32-bit values, but that's a separate discussion. The only problem with using the least significant 8 bits is that they may not be as random as you'd like — they might fail various tests of randomness. How much that matters depends on what you're going to do next. Most likely, you can simply use the code shown. Failing that, you probably need a better pseudo-random number generator (PRNG) thanrand()
.– Jonathan Leffler
Nov 19 '18 at 16:10
1
@JonathanLeffler If you discard some of the random value by a power of 2, it'll still be an even distribution of randomness, won't it? e.g.
rand() % 16
,rand() % 32
,rand() % 64
, etc.– Fiddling Bits
Nov 19 '18 at 16:19
1
See stackoverflow.com/questions/11418113/… and especially links from it, such as to the paper by Marsaglia (sp?).
– Jonathan Leffler
Nov 19 '18 at 16:48
3
@fiddling: if rand is implemented with a.linear congruential generator, then the period of rand%2i is 2i, which is not very random for small
i
.– rici
Nov 19 '18 at 18:32