rand_s()

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

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 on other sites
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 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 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 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 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 on other sites
EDIT: Yeah, screwed that up. See my post below.

Share on other sites
Quote:
 Original post by jpetrieAhh, but it does require a seed; just because you don't explicitly do so doesn't mean

You forgot the part where you link us [grin]

Share on other sites

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

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 19
• 12
• 14
• 9
• Forum Statistics

• Total Topics
633363
• Total Posts
3011509
• Who's Online (See full list)

There are no registered users currently online

×