2001-06-11 21:20:36 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
2003-01-29 14:47:17 +00:00
|
|
|
* File: random.c *
|
2001-06-11 21:20:36 +01:00
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: regular expression interpreter *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
2002-09-09 18:40:12 +01:00
|
|
|
#include "YapInterface.h"
|
2001-06-11 21:20:36 +01:00
|
|
|
#include <math.h>
|
2001-09-10 15:39:32 +01:00
|
|
|
#if defined(__MINGW32__) || _MSC_VER
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2001-06-11 21:20:36 +01:00
|
|
|
|
2017-02-20 15:28:46 +00:00
|
|
|
X_API void init_random( void );
|
2001-06-11 21:20:36 +01:00
|
|
|
|
|
|
|
static short a1 = 27314, b1 = 9213, c1 = 17773;
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-06-11 21:20:36 +01:00
|
|
|
p_random(void)
|
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
double fli;
|
|
|
|
long int t1, t2, t3;
|
2001-06-11 21:20:36 +01:00
|
|
|
|
|
|
|
t1 = (a1 * 171) % 30269;
|
|
|
|
t2 = (b1 * 172) % 30307;
|
|
|
|
t3 = (c1 * 170) % 30323;
|
|
|
|
fli = (t1/30269.0) + (t2/30307.0) + (t3/30323.0);
|
|
|
|
a1 = t1;
|
|
|
|
b1 = t2;
|
|
|
|
c1 = t3;
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG1, YAP_MkFloatTerm(fli-(int)(fli))));
|
2001-06-11 21:20:36 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-06-11 21:20:36 +01:00
|
|
|
p_setrand(void)
|
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
a1 = YAP_IntOfTerm(YAP_ARG1);
|
|
|
|
b1 = YAP_IntOfTerm(YAP_ARG2);
|
|
|
|
c1 = YAP_IntOfTerm(YAP_ARG3);
|
2001-06-11 21:20:36 +01:00
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:02:46 +00:00
|
|
|
static YAP_Bool
|
2001-06-11 21:20:36 +01:00
|
|
|
p_getrand(void)
|
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
return(YAP_Unify(YAP_ARG1,YAP_MkIntTerm(a1)) &&
|
|
|
|
YAP_Unify(YAP_ARG2,YAP_MkIntTerm(b1)) &&
|
|
|
|
YAP_Unify(YAP_ARG3,YAP_MkIntTerm(c1)));
|
2001-06-11 21:20:36 +01:00
|
|
|
}
|
|
|
|
|
2017-02-20 15:28:46 +00:00
|
|
|
X_API void
|
2001-06-11 21:20:36 +01:00
|
|
|
init_random(void)
|
|
|
|
{
|
2002-09-09 18:40:12 +01:00
|
|
|
YAP_UserCPredicate("random", p_random, 1);
|
|
|
|
YAP_UserCPredicate("setrand", p_setrand, 3);
|
|
|
|
YAP_UserCPredicate("getrand", p_getrand, 3);
|
2001-06-11 21:20:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
2014-09-16 19:15:56 +01:00
|
|
|
int WINAPI win_random(HANDLE, DWORD, LPVOID);
|
2001-06-11 21:20:36 +01:00
|
|
|
|
|
|
|
int WINAPI win_random(HANDLE hinst, DWORD reason, LPVOID reserved)
|
|
|
|
{
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
break;
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
break;
|
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
break;
|
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|