USB device can now be unbind
[AROS.git] / compiler / stdc / rand.c
blob0eaffdc2530273f4cfbec54a38013d5240dfa25c
1 /*
2 Copyright © 1995-2016, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 functions rand() and srand().
6 */
8 #include "__stdc_intbase.h"
10 #include <aros/symbolsets.h>
12 /*****************************************************************************
14 NAME */
15 #include <stdlib.h>
17 int rand (
19 /* SYNOPSIS */
20 void)
22 /* FUNCTION
23 A random number generator.
25 INPUTS
26 None.
28 RESULT
29 A pseudo-random integer between 0 and RAND_MAX.
31 NOTES
33 EXAMPLE
35 BUGS
37 SEE ALSO
38 srand()
40 INTERNALS
42 ******************************************************************************/
44 struct StdCIntBase *StdCBase = (struct StdCIntBase *)__aros_getbase_StdCBase();
46 StdCBase->srand_seed = StdCBase->srand_seed * 1103515245 + 12345;
48 return StdCBase->srand_seed % RAND_MAX;
49 } /* rand */
52 /*****************************************************************************
54 NAME */
55 #include <stdlib.h>
57 void srand (
59 /* SYNOPSIS */
60 unsigned int seed)
62 /* FUNCTION
63 Set the starting value for the random number generator rand()
64 If a seed value is set to a value the same stream of pseudo-random
65 numbers will be generated by rand() for the same seed value.
67 INPUTS
68 seed - New start value
70 RESULT
71 None.
73 NOTES
74 One seed value per stdc.library is kept which normally corresponds
75 with per task.
77 EXAMPLE
79 BUGS
81 SEE ALSO
82 rand()
84 INTERNALS
86 ******************************************************************************/
88 struct StdCIntBase *StdCBase = (struct StdCIntBase *)__aros_getbase_StdCBase();
90 StdCBase->srand_seed = seed;
91 } /* srand */
93 static int __rand_seedinit(struct StdCIntBase *StdCBase)
95 // use Xorshift with the suggested coefficients by Marsaglia for
96 // a 32-bit generator (13, 17, 5) to set our initial seed.
97 StdCBase->srand_seed = time(NULL);
98 StdCBase->srand_seed ^= StdCBase->srand_seed >> 13; // a
99 StdCBase->srand_seed ^= StdCBase->srand_seed << 17; // b
100 StdCBase->srand_seed ^= StdCBase->srand_seed >> 5; // c
102 return 1;
105 ADD2OPENLIB(__rand_seedinit, 0);