rand_s() issues

Jan 13, 2011 at 6:06am
I am trying to include rand_s() into my source code.

#define _CRT_RAND_S
#include <cstdlib>

I convinced this would work...

BUT IT DIDN'T!!! ARRGH!

1>.\main.cpp(23) : error C3861: 'rand_s': undeclared identifier

why doesn't this work?

anybody plz help me! OTL
Jan 13, 2011 at 6:10am
Hmmm... is there rand_s() function ? From my Linux I only can see int rand(void) and void srand(unsigned int seed) available for use.

Above assume the default provided cstdlib.
Last edited on Jan 13, 2011 at 6:11am
Jan 13, 2011 at 6:15am
there IS a rand_s() in <stdlib.h>, although it is disabled when _CRT_RAND_S is not defined.
Jan 13, 2011 at 6:23am
Can I know you are on which platform? Linux? Windows? etc etc.
Jan 13, 2011 at 6:29am
I am currently using Windows 7.
Jan 13, 2011 at 6:44am
So most likely they do provide rand_s() function. I am referring to Linux cstdlib.h if you read my post correctly.

For your problem, maybe you need to link to some specific libraries (those that have rand_s() functions inside) on top of those default libraries provided in order to be able to access rand_s() function ?
Jan 24, 2011 at 10:27am
closed account (S6k9GNh0)
What compiler do you use?

http://cplusplus.com/forum/articles/31613/
Last edited on Jan 24, 2011 at 10:43am
Jan 24, 2011 at 12:07pm
This is an example from Visual Studio documentation. Does it wprk ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// crt_rand_s.c
// This program illustrates how to generate random
// integer or floating point numbers in a specified range.

// Remembering to define _CRT_RAND_S prior
// to inclusion statement.
#define _CRT_RAND_S

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

int main( void )
{
    int             i;
    unsigned int    number;
    double          max = 100.0;
    errno_t         err;

    // Display 10 random integers in the range [ 1,10 ].
    for( i = 0; i < 10;i++ )
    {
        err = rand_s( &number );
        if (err != 0)
        {
            printf_s("The rand_s function failed!\n");
        }
        printf_s( "  %u\n", (unsigned int) ((double)number /
                       ((double) UINT_MAX + 1 ) * 10.0) + 1);
    }

    printf_s("\n");

    // Display 10 random doubles in [0, max).
    for (i = 0; i < 10;i++ )
    {
        err = rand_s( &number );
        if (err != 0)
        {
            printf_s("The rand_s function failed!\n");
        }
        printf_s( "  %g\n", (double) number / 
                          ((double) UINT_MAX + 1) * max );
    }
}


Of course rand_s does not work in Linux as it uses internally RtlGenRandom API, which is only available in Windows XP and later.
Last edited on Jan 24, 2011 at 12:09pm
Jan 24, 2011 at 1:11pm
The _s functions in Visual Studio 2005 and beyond are "safe" versions. They are not portable and will only work with Microsoft compilers.

BTW, Microsoft aren't the only ones (or first ones) to provide "safe" versions of standard C library functions. For example, OpenBSD provides "safe" versions of string functions.
Jan 24, 2011 at 4:09pm
how is rand() "unsafe"? what's the point of rand_s()?
Jan 24, 2011 at 4:59pm
I didn't quite see the point either. I never use the "safe" versions of the Microsoft Functions.

I'm generally unsupportive of divergent standards, especially the pointless ones.
Jan 25, 2011 at 12:36am
closed account (S6k9GNh0)
See the post here: http://www.devmaster.net/forums/showthread.php?t=12246
Topic archived. No new replies allowed.