Jump to content
  • Advertisement
Sign in to follow this  

This topic is 4395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using the rand_s() function to create a file filled with random bytes. When I analyze the file to see if any one byte occurs more commonly than another it turns out that the byte 0x0D(13 dec) is about twice as common as any other byte. Can anyone give me a reason for this? Harry J Kuhar

Share this post


Link to post
Share on other sites
Advertisement
I can only think it has to do with the randomizer being either not very random because of your use of it, or because u are not seeding it properly.

Have you got any code to show us?

dave

Share this post


Link to post
Share on other sites
rand_s()? Never heard about it. Show us your code. I suspect there may be an issue of newline translation ("text" vs. "binary" file open modes)

Share this post


Link to post
Share on other sites
rand_s is microsoft's "secure" rand function (regular rand is not since it is not reentrant)

I wouldn't use rand or rand_s for anything serious, try boost::random or mersenne twister.

Share this post


Link to post
Share on other sites
I'm with Fruny. My guess: About half of those 0x13s have 0x10s right next to them, and the file is about 0.4% bigger than it should be. Make sure you're opening the file in binary mode.

Share this post


Link to post
Share on other sites
Thanks everyone for the help, Fruny was correct. Once I added the "b" for binary mode everything was kosher! For those who asked rand_s() is an updated version of rand() from microsoft. Unlike rand it does not require a seed and it generates a full 32-bit random number instead of a 15-bit like rand(). I understand that there are probably better choices out there however I am on the clock and this works.

Thanks for all the help!
H


FILE* Output = fopen(FileName,"wb");

if(NULL == Output)
{
printf("CreatePackeFile Failed");
return;
}

unsigned int PrintDelay = 9999;
unsigned int Value;
for(int Index= 0; Index < Length;Index+=4)
{
PrintDelay++;
rand_s(&Value);
fwrite(&Value,sizeof(Value),1,Output);
if(10000 < PrintDelay)
{
PrintDelay = 0;
printf("Creating File %i/%i\n",Index,Length);
}
}
fclose(Output);


Share this post


Link to post
Share on other sites
*blink*

I didn't mean to even finish that post. I looked the function up, the summary results implied that it needed to be seeded, however after actually digging up the pages it does not (it uses the OS for "cryptographically secure" random values so presumably its getting all seeded and prepped elsewhere). I meant to just hit "back" and discard the post but I goofed up. What a noob. :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!