1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
4 #include <internal/tls.h>
12 thread_data_t
*data
= msvcrt_get_thread_data();
14 /* this is the algorithm used by MSVC, according to
15 * http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators */
16 data
->random_seed
= data
->random_seed
* 214013 + 2531011;
17 return (data
->random_seed
>> 16) & RAND_MAX
;
24 srand(unsigned int seed
)
26 thread_data_t
*data
= msvcrt_get_thread_data();
27 data
->random_seed
= seed
;
30 /*********************************************************************
33 int CDECL
rand_s(unsigned int *pval
)
35 BOOLEAN (WINAPI
*pSystemFunction036
)(PVOID
, ULONG
); // RtlGenRandom
40 _invalid_parameter(NULL
,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__
),__LINE__
, 0);
46 hadvapi32
= LoadLibraryA("advapi32.dll");
49 _invalid_parameter(NULL
,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__
),__LINE__
, 0);
54 pSystemFunction036
= (void*)GetProcAddress(hadvapi32
, "SystemFunction036");
55 if (!pSystemFunction036
)
57 _invalid_parameter(NULL
,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__
),__LINE__
, 0);
59 FreeLibrary(hadvapi32
);
63 if (!pSystemFunction036(pval
, sizeof(*pval
)))
65 _invalid_parameter(NULL
,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__
),__LINE__
, 0);
67 FreeLibrary(hadvapi32
);
71 FreeLibrary(hadvapi32
);