random.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /* Copyright (C) 1999,2000,2001, 2003, 2005, 2006, 2009, 2010 Free Software Foundation, Inc.
  2. * This library is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU Lesser General Public License
  4. * as published by the Free Software Foundation; either version 3 of
  5. * the License, or (at your option) any later version.
  6. *
  7. * This library is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * Lesser General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU Lesser General Public
  13. * License along with this library; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301 USA
  16. */
  17. /* Author: Mikael Djurfeldt <djurfeldt@nada.kth.se> */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include <gmp.h>
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include <string.h>
  26. #include "libguile/smob.h"
  27. #include "libguile/numbers.h"
  28. #include "libguile/feature.h"
  29. #include "libguile/strings.h"
  30. #include "libguile/arrays.h"
  31. #include "libguile/srfi-4.h"
  32. #include "libguile/vectors.h"
  33. #include "libguile/generalized-vectors.h"
  34. #include "libguile/validate.h"
  35. #include "libguile/random.h"
  36. /*
  37. * A plugin interface for RNGs
  38. *
  39. * Using this interface, it is possible for the application to tell
  40. * libguile to use a different RNG. This is desirable if it is
  41. * necessary to use the same RNG everywhere in the application in
  42. * order to prevent interference, if the application uses RNG
  43. * hardware, or if the application has special demands on the RNG.
  44. *
  45. * Look in random.h and how the default generator is "plugged in" in
  46. * scm_init_random().
  47. */
  48. scm_t_rng scm_the_rng;
  49. /*
  50. * The prepackaged RNG
  51. *
  52. * This is the MWC (Multiply With Carry) random number generator
  53. * described by George Marsaglia at the Department of Statistics and
  54. * Supercomputer Computations Research Institute, The Florida State
  55. * University (http://stat.fsu.edu/~geo).
  56. *
  57. * It uses 64 bits, has a period of 4578426017172946943 (4.6e18), and
  58. * passes all tests in the DIEHARD test suite
  59. * (http://stat.fsu.edu/~geo/diehard.html)
  60. */
  61. typedef struct scm_t_i_rstate {
  62. scm_t_rstate rstate;
  63. scm_t_uint32 w;
  64. scm_t_uint32 c;
  65. } scm_t_i_rstate;
  66. #define A 2131995753UL
  67. #ifndef M_PI
  68. #define M_PI 3.14159265359
  69. #endif
  70. static scm_t_uint32
  71. scm_i_uniform32 (scm_t_rstate *state)
  72. {
  73. scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
  74. scm_t_uint64 x = (scm_t_uint64) A * istate->w + istate->c;
  75. scm_t_uint32 w = x & 0xffffffffUL;
  76. istate->w = w;
  77. istate->c = x >> 32L;
  78. return w;
  79. }
  80. static void
  81. scm_i_init_rstate (scm_t_rstate *state, const char *seed, int n)
  82. {
  83. scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
  84. scm_t_uint32 w = 0L;
  85. scm_t_uint32 c = 0L;
  86. int i, m;
  87. for (i = 0; i < n; ++i)
  88. {
  89. m = i % 8;
  90. if (m < 4)
  91. w += seed[i] << (8 * m);
  92. else
  93. c += seed[i] << (8 * (m - 4));
  94. }
  95. if ((w == 0 && c == 0) || (w == -1 && c == A - 1))
  96. ++c;
  97. istate->w = w;
  98. istate->c = c;
  99. }
  100. static scm_t_rstate *
  101. scm_i_copy_rstate (scm_t_rstate *state)
  102. {
  103. scm_t_rstate *new_state;
  104. new_state = scm_gc_malloc_pointerless (state->rng->rstate_size,
  105. "random-state");
  106. return memcpy (new_state, state, state->rng->rstate_size);
  107. }
  108. SCM_SYMBOL(scm_i_rstate_tag, "multiply-with-carry");
  109. static void
  110. scm_i_rstate_from_datum (scm_t_rstate *state, SCM value)
  111. #define FUNC_NAME "scm_i_rstate_from_datum"
  112. {
  113. scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
  114. scm_t_uint32 w, c;
  115. long length;
  116. SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, value, length);
  117. SCM_ASSERT (length == 3, value, SCM_ARG1, FUNC_NAME);
  118. SCM_ASSERT (scm_is_eq (SCM_CAR (value), scm_i_rstate_tag),
  119. value, SCM_ARG1, FUNC_NAME);
  120. SCM_VALIDATE_UINT_COPY (SCM_ARG1, SCM_CADR (value), w);
  121. SCM_VALIDATE_UINT_COPY (SCM_ARG1, SCM_CADDR (value), c);
  122. istate->w = w;
  123. istate->c = c;
  124. }
  125. #undef FUNC_NAME
  126. static SCM
  127. scm_i_rstate_to_datum (scm_t_rstate *state)
  128. {
  129. scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
  130. return scm_list_3 (scm_i_rstate_tag,
  131. scm_from_uint32 (istate->w),
  132. scm_from_uint32 (istate->c));
  133. }
  134. /*
  135. * Random number library functions
  136. */
  137. scm_t_rstate *
  138. scm_c_make_rstate (const char *seed, int n)
  139. {
  140. scm_t_rstate *state;
  141. state = scm_gc_malloc_pointerless (scm_the_rng.rstate_size,
  142. "random-state");
  143. state->rng = &scm_the_rng;
  144. state->normal_next = 0.0;
  145. state->rng->init_rstate (state, seed, n);
  146. return state;
  147. }
  148. scm_t_rstate *
  149. scm_c_rstate_from_datum (SCM datum)
  150. {
  151. scm_t_rstate *state;
  152. state = scm_gc_malloc_pointerless (scm_the_rng.rstate_size,
  153. "random-state");
  154. state->rng = &scm_the_rng;
  155. state->normal_next = 0.0;
  156. state->rng->from_datum (state, datum);
  157. return state;
  158. }
  159. scm_t_rstate *
  160. scm_c_default_rstate ()
  161. #define FUNC_NAME "scm_c_default_rstate"
  162. {
  163. SCM state = SCM_VARIABLE_REF (scm_var_random_state);
  164. if (!SCM_RSTATEP (state))
  165. SCM_MISC_ERROR ("*random-state* contains bogus random state", SCM_EOL);
  166. return SCM_RSTATE (state);
  167. }
  168. #undef FUNC_NAME
  169. double
  170. scm_c_uniform01 (scm_t_rstate *state)
  171. {
  172. double x = (double) state->rng->random_bits (state) / (double) 0xffffffffUL;
  173. return ((x + (double) state->rng->random_bits (state))
  174. / (double) 0xffffffffUL);
  175. }
  176. double
  177. scm_c_normal01 (scm_t_rstate *state)
  178. {
  179. if (state->normal_next != 0.0)
  180. {
  181. double ret = state->normal_next;
  182. state->normal_next = 0.0;
  183. return ret;
  184. }
  185. else
  186. {
  187. double r, a, n;
  188. r = sqrt (-2.0 * log (scm_c_uniform01 (state)));
  189. a = 2.0 * M_PI * scm_c_uniform01 (state);
  190. n = r * sin (a);
  191. state->normal_next = r * cos (a);
  192. return n;
  193. }
  194. }
  195. double
  196. scm_c_exp1 (scm_t_rstate *state)
  197. {
  198. return - log (scm_c_uniform01 (state));
  199. }
  200. unsigned char scm_masktab[256];
  201. static inline scm_t_uint32
  202. scm_i_mask32 (scm_t_uint32 m)
  203. {
  204. return (m < 0x100
  205. ? scm_masktab[m]
  206. : (m < 0x10000
  207. ? scm_masktab[m >> 8] << 8 | 0xff
  208. : (m < 0x1000000
  209. ? scm_masktab[m >> 16] << 16 | 0xffff
  210. : scm_masktab[m >> 24] << 24 | 0xffffff)));
  211. }
  212. scm_t_uint32
  213. scm_c_random (scm_t_rstate *state, scm_t_uint32 m)
  214. {
  215. scm_t_uint32 r, mask = scm_i_mask32 (m);
  216. while ((r = state->rng->random_bits (state) & mask) >= m);
  217. return r;
  218. }
  219. scm_t_uint64
  220. scm_c_random64 (scm_t_rstate *state, scm_t_uint64 m)
  221. {
  222. scm_t_uint64 r;
  223. scm_t_uint32 mask;
  224. if (m <= SCM_T_UINT32_MAX)
  225. return scm_c_random (state, (scm_t_uint32) m);
  226. mask = scm_i_mask32 (m >> 32);
  227. while ((r = ((scm_t_uint64) (state->rng->random_bits (state) & mask) << 32)
  228. | state->rng->random_bits (state)) >= m)
  229. ;
  230. return r;
  231. }
  232. /*
  233. SCM scm_c_random_bignum (scm_t_rstate *state, SCM m)
  234. Takes a random state (source of random bits) and a bignum m.
  235. Returns a bignum b, 0 <= b < m.
  236. It does this by allocating a bignum b with as many base 65536 digits
  237. as m, filling b with random bits (in 32 bit chunks) up to the most
  238. significant 1 in m, and, finally checking if the resultant b is too
  239. large (>= m). If too large, we simply repeat the process again. (It
  240. is important to throw away all generated random bits if b >= m,
  241. otherwise we'll end up with a distorted distribution.)
  242. */
  243. SCM
  244. scm_c_random_bignum (scm_t_rstate *state, SCM m)
  245. {
  246. SCM result = scm_i_mkbig ();
  247. const size_t m_bits = mpz_sizeinbase (SCM_I_BIG_MPZ (m), 2);
  248. /* how many bits would only partially fill the last scm_t_uint32? */
  249. const size_t end_bits = m_bits % (sizeof (scm_t_uint32) * SCM_CHAR_BIT);
  250. scm_t_uint32 *random_chunks = NULL;
  251. const scm_t_uint32 num_full_chunks =
  252. m_bits / (sizeof (scm_t_uint32) * SCM_CHAR_BIT);
  253. const scm_t_uint32 num_chunks = num_full_chunks + ((end_bits) ? 1 : 0);
  254. /* we know the result will be this big */
  255. mpz_realloc2 (SCM_I_BIG_MPZ (result), m_bits);
  256. random_chunks =
  257. (scm_t_uint32 *) scm_gc_calloc (num_chunks * sizeof (scm_t_uint32),
  258. "random bignum chunks");
  259. do
  260. {
  261. scm_t_uint32 *current_chunk = random_chunks + (num_chunks - 1);
  262. scm_t_uint32 chunks_left = num_chunks;
  263. mpz_set_ui (SCM_I_BIG_MPZ (result), 0);
  264. if (end_bits)
  265. {
  266. /* generate a mask with ones in the end_bits position, i.e. if
  267. end_bits is 3, then we'd have a mask of ...0000000111 */
  268. const scm_t_uint32 rndbits = state->rng->random_bits (state);
  269. int rshift = (sizeof (scm_t_uint32) * SCM_CHAR_BIT) - end_bits;
  270. scm_t_uint32 mask = ((scm_t_uint32)-1) >> rshift;
  271. scm_t_uint32 highest_bits = rndbits & mask;
  272. *current_chunk-- = highest_bits;
  273. chunks_left--;
  274. }
  275. while (chunks_left)
  276. {
  277. /* now fill in the remaining scm_t_uint32 sized chunks */
  278. *current_chunk-- = state->rng->random_bits (state);
  279. chunks_left--;
  280. }
  281. mpz_import (SCM_I_BIG_MPZ (result),
  282. num_chunks,
  283. -1,
  284. sizeof (scm_t_uint32),
  285. 0,
  286. 0,
  287. random_chunks);
  288. /* if result >= m, regenerate it (it is important to regenerate
  289. all bits in order not to get a distorted distribution) */
  290. } while (mpz_cmp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (m)) >= 0);
  291. scm_gc_free (random_chunks,
  292. num_chunks * sizeof (scm_t_uint32),
  293. "random bignum chunks");
  294. return scm_i_normbig (result);
  295. }
  296. /*
  297. * Scheme level representation of random states.
  298. */
  299. scm_t_bits scm_tc16_rstate;
  300. static SCM
  301. make_rstate (scm_t_rstate *state)
  302. {
  303. SCM_RETURN_NEWSMOB (scm_tc16_rstate, state);
  304. }
  305. /*
  306. * Scheme level interface.
  307. */
  308. SCM_GLOBAL_VARIABLE_INIT (scm_var_random_state, "*random-state*", scm_seed_to_random_state (scm_from_locale_string ("URL:http://stat.fsu.edu/~geo/diehard.html")));
  309. SCM_DEFINE (scm_random, "random", 1, 1, 0,
  310. (SCM n, SCM state),
  311. "Return a number in [0, N).\n"
  312. "\n"
  313. "Accepts a positive integer or real n and returns a\n"
  314. "number of the same type between zero (inclusive) and\n"
  315. "N (exclusive). The values returned have a uniform\n"
  316. "distribution.\n"
  317. "\n"
  318. "The optional argument @var{state} must be of the type produced\n"
  319. "by @code{seed->random-state}. It defaults to the value of the\n"
  320. "variable @var{*random-state*}. This object is used to maintain\n"
  321. "the state of the pseudo-random-number generator and is altered\n"
  322. "as a side effect of the random operation.")
  323. #define FUNC_NAME s_scm_random
  324. {
  325. if (SCM_UNBNDP (state))
  326. state = SCM_VARIABLE_REF (scm_var_random_state);
  327. SCM_VALIDATE_RSTATE (2, state);
  328. if (SCM_I_INUMP (n))
  329. {
  330. scm_t_bits m = (scm_t_bits) SCM_I_INUM (n);
  331. SCM_ASSERT_RANGE (1, n, SCM_I_INUM (n) > 0);
  332. #if SCM_SIZEOF_UINTPTR_T <= 4
  333. return scm_from_uint32 (scm_c_random (SCM_RSTATE (state),
  334. (scm_t_uint32) m));
  335. #elif SCM_SIZEOF_UINTPTR_T <= 8
  336. return scm_from_uint64 (scm_c_random64 (SCM_RSTATE (state),
  337. (scm_t_uint64) m));
  338. #else
  339. #error "Cannot deal with this platform's scm_t_bits size"
  340. #endif
  341. }
  342. SCM_VALIDATE_NIM (1, n);
  343. if (SCM_REALP (n))
  344. return scm_from_double (SCM_REAL_VALUE (n)
  345. * scm_c_uniform01 (SCM_RSTATE (state)));
  346. if (!SCM_BIGP (n))
  347. SCM_WRONG_TYPE_ARG (1, n);
  348. return scm_c_random_bignum (SCM_RSTATE (state), n);
  349. }
  350. #undef FUNC_NAME
  351. SCM_DEFINE (scm_copy_random_state, "copy-random-state", 0, 1, 0,
  352. (SCM state),
  353. "Return a copy of the random state @var{state}.")
  354. #define FUNC_NAME s_scm_copy_random_state
  355. {
  356. if (SCM_UNBNDP (state))
  357. state = SCM_VARIABLE_REF (scm_var_random_state);
  358. SCM_VALIDATE_RSTATE (1, state);
  359. return make_rstate (SCM_RSTATE (state)->rng->copy_rstate (SCM_RSTATE (state)));
  360. }
  361. #undef FUNC_NAME
  362. SCM_DEFINE (scm_seed_to_random_state, "seed->random-state", 1, 0, 0,
  363. (SCM seed),
  364. "Return a new random state using @var{seed}.")
  365. #define FUNC_NAME s_scm_seed_to_random_state
  366. {
  367. SCM res;
  368. if (SCM_NUMBERP (seed))
  369. seed = scm_number_to_string (seed, SCM_UNDEFINED);
  370. SCM_VALIDATE_STRING (1, seed);
  371. res = make_rstate (scm_c_make_rstate (scm_i_string_chars (seed),
  372. scm_i_string_length (seed)));
  373. scm_remember_upto_here_1 (seed);
  374. return res;
  375. }
  376. #undef FUNC_NAME
  377. SCM_DEFINE (scm_datum_to_random_state, "datum->random-state", 1, 0, 0,
  378. (SCM datum),
  379. "Return a new random state using @var{datum}, which should have\n"
  380. "been obtained from @code{random-state->datum}.")
  381. #define FUNC_NAME s_scm_datum_to_random_state
  382. {
  383. return make_rstate (scm_c_rstate_from_datum (datum));
  384. }
  385. #undef FUNC_NAME
  386. SCM_DEFINE (scm_random_state_to_datum, "random-state->datum", 1, 0, 0,
  387. (SCM state),
  388. "Return a datum representation of @var{state} that may be\n"
  389. "written out and read back with the Scheme reader.")
  390. #define FUNC_NAME s_scm_random_state_to_datum
  391. {
  392. SCM_VALIDATE_RSTATE (1, state);
  393. return SCM_RSTATE (state)->rng->to_datum (SCM_RSTATE (state));
  394. }
  395. #undef FUNC_NAME
  396. SCM_DEFINE (scm_random_uniform, "random:uniform", 0, 1, 0,
  397. (SCM state),
  398. "Return a uniformly distributed inexact real random number in\n"
  399. "[0,1).")
  400. #define FUNC_NAME s_scm_random_uniform
  401. {
  402. if (SCM_UNBNDP (state))
  403. state = SCM_VARIABLE_REF (scm_var_random_state);
  404. SCM_VALIDATE_RSTATE (1, state);
  405. return scm_from_double (scm_c_uniform01 (SCM_RSTATE (state)));
  406. }
  407. #undef FUNC_NAME
  408. SCM_DEFINE (scm_random_normal, "random:normal", 0, 1, 0,
  409. (SCM state),
  410. "Return an inexact real in a normal distribution. The\n"
  411. "distribution used has mean 0 and standard deviation 1. For a\n"
  412. "normal distribution with mean m and standard deviation d use\n"
  413. "@code{(+ m (* d (random:normal)))}.")
  414. #define FUNC_NAME s_scm_random_normal
  415. {
  416. if (SCM_UNBNDP (state))
  417. state = SCM_VARIABLE_REF (scm_var_random_state);
  418. SCM_VALIDATE_RSTATE (1, state);
  419. return scm_from_double (scm_c_normal01 (SCM_RSTATE (state)));
  420. }
  421. #undef FUNC_NAME
  422. static void
  423. vector_scale_x (SCM v, double c)
  424. {
  425. size_t n;
  426. if (scm_is_simple_vector (v))
  427. {
  428. n = SCM_SIMPLE_VECTOR_LENGTH (v);
  429. while (n-- > 0)
  430. SCM_REAL_VALUE (SCM_SIMPLE_VECTOR_REF (v, n)) *= c;
  431. }
  432. else
  433. {
  434. /* must be a f64vector. */
  435. scm_t_array_handle handle;
  436. size_t i, len;
  437. ssize_t inc;
  438. double *elts;
  439. elts = scm_f64vector_writable_elements (v, &handle, &len, &inc);
  440. for (i = 0; i < len; i++, elts += inc)
  441. *elts *= c;
  442. scm_array_handle_release (&handle);
  443. }
  444. }
  445. static double
  446. vector_sum_squares (SCM v)
  447. {
  448. double x, sum = 0.0;
  449. size_t n;
  450. if (scm_is_simple_vector (v))
  451. {
  452. n = SCM_SIMPLE_VECTOR_LENGTH (v);
  453. while (n-- > 0)
  454. {
  455. x = SCM_REAL_VALUE (SCM_SIMPLE_VECTOR_REF (v, n));
  456. sum += x * x;
  457. }
  458. }
  459. else
  460. {
  461. /* must be a f64vector. */
  462. scm_t_array_handle handle;
  463. size_t i, len;
  464. ssize_t inc;
  465. const double *elts;
  466. elts = scm_f64vector_elements (v, &handle, &len, &inc);
  467. for (i = 0; i < len; i++, elts += inc)
  468. {
  469. x = *elts;
  470. sum += x * x;
  471. }
  472. scm_array_handle_release (&handle);
  473. }
  474. return sum;
  475. }
  476. /* For the uniform distribution on the solid sphere, note that in
  477. * this distribution the length r of the vector has cumulative
  478. * distribution r^n; i.e., u=r^n is uniform [0,1], so r can be
  479. * generated as r=u^(1/n).
  480. */
  481. SCM_DEFINE (scm_random_solid_sphere_x, "random:solid-sphere!", 1, 1, 0,
  482. (SCM v, SCM state),
  483. "Fills @var{vect} with inexact real random numbers the sum of\n"
  484. "whose squares is less than 1.0. Thinking of @var{vect} as\n"
  485. "coordinates in space of dimension @var{n} @math{=}\n"
  486. "@code{(vector-length @var{vect})}, the coordinates are\n"
  487. "uniformly distributed within the unit @var{n}-sphere.")
  488. #define FUNC_NAME s_scm_random_solid_sphere_x
  489. {
  490. if (SCM_UNBNDP (state))
  491. state = SCM_VARIABLE_REF (scm_var_random_state);
  492. SCM_VALIDATE_RSTATE (2, state);
  493. scm_random_normal_vector_x (v, state);
  494. vector_scale_x (v,
  495. pow (scm_c_uniform01 (SCM_RSTATE (state)),
  496. 1.0 / scm_c_generalized_vector_length (v))
  497. / sqrt (vector_sum_squares (v)));
  498. return SCM_UNSPECIFIED;
  499. }
  500. #undef FUNC_NAME
  501. SCM_DEFINE (scm_random_hollow_sphere_x, "random:hollow-sphere!", 1, 1, 0,
  502. (SCM v, SCM state),
  503. "Fills vect with inexact real random numbers\n"
  504. "the sum of whose squares is equal to 1.0.\n"
  505. "Thinking of vect as coordinates in space of\n"
  506. "dimension n = (vector-length vect), the coordinates\n"
  507. "are uniformly distributed over the surface of the\n"
  508. "unit n-sphere.")
  509. #define FUNC_NAME s_scm_random_hollow_sphere_x
  510. {
  511. if (SCM_UNBNDP (state))
  512. state = SCM_VARIABLE_REF (scm_var_random_state);
  513. SCM_VALIDATE_RSTATE (2, state);
  514. scm_random_normal_vector_x (v, state);
  515. vector_scale_x (v, 1 / sqrt (vector_sum_squares (v)));
  516. return SCM_UNSPECIFIED;
  517. }
  518. #undef FUNC_NAME
  519. SCM_DEFINE (scm_random_normal_vector_x, "random:normal-vector!", 1, 1, 0,
  520. (SCM v, SCM state),
  521. "Fills vect with inexact real random numbers that are\n"
  522. "independent and standard normally distributed\n"
  523. "(i.e., with mean 0 and variance 1).")
  524. #define FUNC_NAME s_scm_random_normal_vector_x
  525. {
  526. long i;
  527. scm_t_array_handle handle;
  528. scm_t_array_dim *dim;
  529. if (SCM_UNBNDP (state))
  530. state = SCM_VARIABLE_REF (scm_var_random_state);
  531. SCM_VALIDATE_RSTATE (2, state);
  532. scm_generalized_vector_get_handle (v, &handle);
  533. dim = scm_array_handle_dims (&handle);
  534. if (scm_is_vector (v))
  535. {
  536. SCM *elts = scm_array_handle_writable_elements (&handle);
  537. for (i = dim->lbnd; i <= dim->ubnd; i++, elts += dim->inc)
  538. *elts = scm_from_double (scm_c_normal01 (SCM_RSTATE (state)));
  539. }
  540. else
  541. {
  542. /* must be a f64vector. */
  543. double *elts = scm_array_handle_f64_writable_elements (&handle);
  544. for (i = dim->lbnd; i <= dim->ubnd; i++, elts += dim->inc)
  545. *elts = scm_c_normal01 (SCM_RSTATE (state));
  546. }
  547. scm_array_handle_release (&handle);
  548. return SCM_UNSPECIFIED;
  549. }
  550. #undef FUNC_NAME
  551. SCM_DEFINE (scm_random_exp, "random:exp", 0, 1, 0,
  552. (SCM state),
  553. "Return an inexact real in an exponential distribution with mean\n"
  554. "1. For an exponential distribution with mean u use (* u\n"
  555. "(random:exp)).")
  556. #define FUNC_NAME s_scm_random_exp
  557. {
  558. if (SCM_UNBNDP (state))
  559. state = SCM_VARIABLE_REF (scm_var_random_state);
  560. SCM_VALIDATE_RSTATE (1, state);
  561. return scm_from_double (scm_c_exp1 (SCM_RSTATE (state)));
  562. }
  563. #undef FUNC_NAME
  564. void
  565. scm_init_random ()
  566. {
  567. int i, m;
  568. /* plug in default RNG */
  569. scm_t_rng rng =
  570. {
  571. sizeof (scm_t_i_rstate),
  572. scm_i_uniform32,
  573. scm_i_init_rstate,
  574. scm_i_copy_rstate,
  575. scm_i_rstate_from_datum,
  576. scm_i_rstate_to_datum
  577. };
  578. scm_the_rng = rng;
  579. scm_tc16_rstate = scm_make_smob_type ("random-state", 0);
  580. for (m = 1; m <= 0x100; m <<= 1)
  581. for (i = m >> 1; i < m; ++i)
  582. scm_masktab[i] = m - 1;
  583. #include "libguile/random.x"
  584. scm_add_feature ("random");
  585. }
  586. /*
  587. Local Variables:
  588. c-file-style: "gnu"
  589. End:
  590. */