1 | [[tags: manual]] |
---|

2 | [[toc:]] |
---|

3 | |
---|

4 | == Module (chicken random) |
---|

5 | |
---|

6 | This module provides access to entropy (as provided by the operating |
---|

7 | system) and a pseudo random number generator. |
---|

8 | |
---|

9 | On UNIX systems entropy is by default obtained from {{/dev/urandom}}. |
---|

10 | On Linux, the {{getrandom(2)}} system call is used instead, if available. |
---|

11 | On OpenBSD it is {{arc4random_buf(3)}}, on Windows {{RtlGenRandom}}. |
---|

12 | |
---|

13 | The pseudo random number generator is an implementation of the |
---|

14 | [[WELL512|https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linear]] algorithm. |
---|

15 | |
---|

16 | ==== set-pseudo-random-seed! |
---|

17 | |
---|

18 | <procedure>(set-pseudo-random-seed! SEED [SIZE])</procedure> |
---|

19 | |
---|

20 | Set seed for the PRNG from the at most {{SIZE}} bytes of {{SEED}}. |
---|

21 | which should be a byte vector or a string containing random data. |
---|

22 | If {{SIZE}} is not given, it defaults to the size of {{SEED}}. If {{SIZE}} |
---|

23 | (or the size of {{SEED}}) is below the required size of the random |
---|

24 | state used by the PRNG, then it will necessarily be initialized in a less |
---|

25 | random manner as possible. |
---|

26 | |
---|

27 | |
---|

28 | ==== pseudo-random-integer |
---|

29 | |
---|

30 | <procedure>(pseudo-random-integer [RANGE])</procedure> |
---|

31 | |
---|

32 | Returns a uniformly distributed pseudo random integer between 0 and |
---|

33 | {{RANGE-1}}, which may be a big integer. |
---|

34 | |
---|

35 | |
---|

36 | ==== pseudo-random-real |
---|

37 | |
---|

38 | <procedure>(pseudo-random-real)</procedure> |
---|

39 | |
---|

40 | Returns a uniformly distributed pseudo-random inexact number between |
---|

41 | 0 and 1. |
---|

42 | |
---|

43 | |
---|

44 | ==== random-bytes |
---|

45 | |
---|

46 | <procedure>(random-bytes [BUF [SIZE]])</procedure> |
---|

47 | |
---|

48 | Returns random bytes from the available entropy source. If {{BUF}} is |
---|

49 | given, it should be a string or byte vector which will be filled with up to |
---|

50 | {{SIZE}} random bytes. {{SIZE}} defaults to the size of the {{BUF}} |
---|

51 | argument. If no arguments are given, {{random-bytes}} returns a |
---|

52 | freshly allocated byte vector of sufficient size to be used as a seed |
---|

53 | for {{set-pseudo-random-seed!}}. |
---|

54 | |
---|

55 | |
---|

56 | --- |
---|

57 | Previous: [[Module (chicken process-context)]] |
---|

58 | |
---|

59 | Next: [[Module (chicken read-syntax)]] |
---|