Linear congruential generator: Difference between revisions
Content added Content deleted
(Added PicoLisp) |
|||
Line 34: | Line 34: | ||
(Here should be some sample numbers for a few seeds, so that one who solves this task can check if one's numbers matches these samples.) |
(Here should be some sample numbers for a few seeds, so that one who solves this task can check if one's numbers matches these samples.) |
||
=={{header|C}}== |
|||
In a pretended lib style, this code produces a rand() function depends on compiler macro: <code>gcc -DMS_RAND</code> uses MS style, otherwise it's BSD rand by default. |
|||
<lang C>#include <stdio.h> |
|||
/* always assuming int is at least 32 bits */ |
|||
int rand(); |
|||
int rseed = 0; |
|||
inline void srand(int x) |
|||
{ |
|||
rseed = x; |
|||
} |
|||
#ifndef MS_RAND |
|||
#define RAND_MAX ((1 << 31) - 1) |
|||
inline int rand() |
|||
{ |
|||
return rseed = (rseed * 1103515245 + 12345) & RAND_MAX; |
|||
} |
|||
#else /* MS rand */ |
|||
#define RAND_MAX_32 ((1 << 31) - 1) |
|||
#define RAND_MAX ((1 << 15) - 1) |
|||
inline int rand() |
|||
{ |
|||
return (rseed = (rseed * 214013 + 2531011) & RAND_MAX_32) >> 16; |
|||
} |
|||
#endif/* MS_RAND */ |
|||
int main() |
|||
{ |
|||
int i; |
|||
printf("rand max is %d\n", RAND_MAX); |
|||
for (i = 0; i < 100; i++) |
|||
printf("%d\n", rand()); |
|||
return 0; |
|||
}</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
The following were written before the task was complete and may need adjustment. |
The following were written before the task was complete and may need adjustment. |