How to generate an 8-bit random number in C>












-1














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?










share|improve this question




















  • 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 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




    @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
















-1














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?










share|improve this question




















  • 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 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




    @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














-1












-1








-1







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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




    @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














  • 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 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




    @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








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












2 Answers
2






active

oldest

votes


















2















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.






share|improve this answer























  • 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



















0














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.






share|improve this answer





















  • 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











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%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









2















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.






share|improve this answer























  • 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
















2















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.






share|improve this answer























  • 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














2












2








2







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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













0














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.






share|improve this answer





















  • 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
















0














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.






share|improve this answer





















  • 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














0












0








0






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 16:42









Susheel Dwivedi

554




554












  • 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


















  • 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
















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


















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.





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.




draft saved


draft discarded














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





















































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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith