File: mpz.rst

package info (click to toggle)
postgresql-pgmp 1.0.5-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 556 kB
  • sloc: ansic: 2,059; sql: 853; python: 591; makefile: 101; sh: 15
file content (714 lines) | stat: -rw-r--r-- 24,154 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
`!mpz` data type
================

The `!mpz` data type can store integer numbers of arbitrary size, up to the
maximum memory allowed by PostgreSQL data types (about 1GB).

Every PostgreSQL integer type (`!int16`, `!int32`, `!int64`) can be converted
to `!mpz`. Not integer types (`!float4`, `!float8`, `!numeric`) are truncated.
Cast from integer types are automatic, whereas non integer require explicit
cast (but are implicitly converted in assignment).

.. code-block:: postgres

    SELECT mpz(10000);      -- Cast using mpz as a function
    SELECT 10000::mpz;      -- PostgreSQL-style cast

Casting `!mpz` to PostgreSQL integer types and `!numeric` works as expected
and will overflow if the value is too big for their range. Casting to
`!float4` and `!float8` works as well: in case of overflow the value will be
*Infinity*.

`!mpz` values can be compared using the regular PostgreSQL comparison
operators. Indexes on `!mpz` columns can be created using the *btree* or the
*hash* method.


`!mpz` textual input/output
---------------------------

.. function:: mpz(text)
              mpz(text, base)

    Convert a textual representation into an `!mpz` number. The form
    :samp:`{text}::mpz` is equivalent to :samp:`mpz({text})`.

    White space is allowed in the string, and is simply ignored.

    The *base* may vary from 2 to 62, or if *base* is 0 or omitted, then the
    leading characters are used: ``0x`` and ``0X`` for hexadecimal, ``0b`` and
    ``0B`` for binary, ``0`` for octal, or decimal otherwise.

    For bases up to 36, case is ignored; upper-case and lower-case letters
    have the same value.  For bases 37 to 62, upper-case letter represent the
    usual 10..35 while lower-case letter represent 36..61.

    .. code-block:: psql

        =# SELECT '0x10'::mpz AS "hex", '10'::mpz AS "dec",
        -#        '010'::mpz AS "oct", '0b10'::mpz AS "bin";
         hex | dec | oct | bin
        -----+-----+-----+-----
         16  | 10  | 8   | 2


    .. note:: The maximum base accepted by GMP 4.1 is 36, not 62.


.. function:: text(z)
              text(z, base)

    Convert the `!mpz` *z* into a string. The form :samp:`{z}::text` is
    equivalent to :samp:`text({z})`.

    *base* may vary from 2 to 62 or from -2 to -36.  For base in the range
    2..36, digits and lower-case letters are used; for -2..-36, digits and
    upper-case letters are used; for 37..62, digits, upper-case letters, and
    lower-case letters (in that significance order) are used. If *base* is not
    specified, 10 is assumed.

    .. note:: The maximum base accepted by GMP 4.1 is 36, not 62.


Arithmetic Operators and Functions
----------------------------------

These operators can either work on `!mpz` arguments or take an integer
argument that will be implicitly converted.  Operators taking a :math:`2^n`
argument always use an integer as right argument.

.. note::
    GMP defines many structures in terms of `!long` or `!unsigned long`, whose
    definitions may vary across platforms. PostgreSQL instead offers data
    types with a defined number of bytes (e.g. `!int4`, `!int8`). For this
    reason, functions taking an integer as argument are defined as `!int8`,
    but they may actually fail if the server is 32 bit and the argument
    doesn't fit into an `!int4`.

.. table:: Arithmetic operators

    =========== =============================== =================== ===========
    Operator    Description                     Example             Return
    =========== =============================== =================== ===========
    `!-`        Unary minus                     `!- 5::mpz`         -5
    `!+`        Unary plus                      `!+ 5::mpz`         5
    `!+`        Addition                        `!2::mpz + 3::mpz`  5
    `!-`        Subtraction                     `!2::mpz - 3::mpz`  -1
    `!*`        Multiplication                  `!7::mpz * 3::mpz`  21
    `!<<`       Multiplication by :math:`2^n`   `!3::mpz << 2`      12
    `!^`        Power (1)                       `!3::mpz ^ 2`       9
    =========== =============================== =================== ===========

Notes:

(1)
    See also the `exponentiation functions`_.


.. function:: abs(z)

    Return the absolute value of *z*.


.. function:: sgn(z)

    Return +1 if *z* > 0, 0 if *z* = 0, and -1 if *z* < 0.


.. function:: odd(z)
              even(z)

    Return `!true` if *z* is odd or even, respectively, else `!false`.


Division Operators and Functions
--------------------------------

For all the division-related operators :math:`n \oslash d`, :math:`q` and
:math:`r` will satisfy :math:`n = q \cdot d + r`, and :math:`r` will satisfy
:math:`0 \le |r| \lt |d|`.

.. note::
    Only the truncating division and reminder (`!/` and `!%`) have the correct
    precedence respect to addition, subtraction and multiplication.
    See `the PostgreSQL precedence table`__ for further details.

    .. __: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-PRECEDENCE-TABLE

..
    note: this table contains non-breaking spaces to align the - signs.

.. table:: Division operators

    =========== =============================== ==================== =======
    Operator    Description                     Example              Return
    =========== =============================== ==================== =======
    `!/`        Division quotient               `! 7::mpz / 3::mpz`  2

                Rounding towards zero           `!-7::mpz / 3::mpz`  -2

    `!%`        Division reminder               `! 7::mpz % 3::mpz`  1

                Rounding towards zero           `!-7::mpz % 3::mpz`  -1

    `+/`        Division quotient               `! 7::mpz +/ 3::mpz` 3

                Rounding towards +infinity      `!-7::mpz +/ 3::mpz` -2

    `+%`        Division reminder               `! 7::mpz +% 3::mpz` -2

                Rounding towards +infinity      `!-7::mpz +% 3::mpz` -1

    `!-/`       Division quotient               `! 7::mpz -/ 3::mpz` 2

                Rounding towards -infinity      `!-7::mpz -/ 3::mpz` -3

    `!-%`       Division reminder               `! 7::mpz -% 3::mpz` 1

                Rounding towards -infinity      `!-7::mpz -% 3::mpz` 2

    `/?`        Divisible (1)                   `!21::mpz /? 7::mpz` `!true`

    `/!`        Exact division (2)              `!21::mpz /! 7::mpz` 3
    =========== =============================== ==================== =======

Notes:

(1)
    See also the function `divisible()`.

(2)
    The exact division operator (`!/!`) produces correct results only when it
    is known in advance that :math:`d` divides :math:`n`.  The operator is
    much faster than the other division operators, and is the best choice when
    exact division is known to occur, for example reducing a rational to
    lowest terms.

..
    note: this table contains non-breaking spaces to align the - signs.

.. table:: Division operators for powers of 2

    ======== ==================================== =================== =======
    Operator Description                          Example             Return
    ======== ==================================== =================== =======
    `!>>`    Quotient of division by :math:`2^n`  `! 1027::mpz >> 3`  128

             Rounding towards zero                `!-1027::mpz >> 3`  -128

    `!%>`    Remainder of division by :math:`2^n` `! 1027::mpz %> 3`  3

             Rounding towards zero                `!-1027::mpz %> 3`  -3

    `!+>>`   Quotient of division by :math:`2^n`  `! 1027::mpz +>> 3` 129

             Rounding towards +infinity           `!-1027::mpz +>> 3` -128

    `!+%>`   Remainder of division by :math:`2^n` `! 1027::mpz +%> 3` -5

             Rounding towards +infinity           `!-1027::mpz +%> 3` -3

    `!->>`   Quotient of division by :math:`2^n`  `! 1027::mpz ->> 3` 128

             Rounding towards -infinity           `!-1027::mpz ->> 3` -129

    `!-%>`   Remainder of division by :math:`2^n` `! 1027::mpz -%> 3` 3

             Rounding towards -infinity           `!-1027::mpz -%> 3` 5

    `>>?`    Divisible by :math:`2^n` (1)         `!64::mpz >>? 3`    `!true`
    ======== ==================================== =================== =======

(1)
    See also the function `divisible_2exp()`.


.. function:: tdiv_qr(n, d)

    Return a tuple containing quotient *q* and remainder *r* of the division,
    rounding towards 0.


.. function:: cdiv_qr(n, d)

    Return a tuple containing quotient *q* and remainder *r* of the division,
    rounding towards +infinity (ceil).


.. function:: fdiv_qr(n, d)

    Return a tuple containing quotient *q* and remainder *r* of the division,
    rounding towards -infinity (floor).



.. function:: divisible(n, d)
              divisible_2exp(n, b)

    Return `!true` if *n* is exactly divisible by *d*, or in the case of
    `!divisible_2exp()` by :math:`2^b`.

    :math:`n` is divisible by :math:`d` if there exists an integer :math:`q`
    satisfying :math:`n = q \cdot d`.  Unlike the other division operators,
    *d*\=0 is accepted and following the rule it can be seen that only 0
    is considered divisible by 0.

    The operators `!/?` and `!>>?` are aliases for `!divisible()` and
    `!divisible_2exp()`.


.. function:: congruent(n, c, d)
              congruent_2exp(n, c, b)

    Return `!true` if *n* is congruent to *c* modulo *d*, or in the case of
    `!congruent_2exp()` modulo :math:`2^b`.

    :math:`n` is congruent to :math:`c \mod d` if there exists an integer
    :math:`q` satisfying :math:`n = c + q \cdot d`. Unlike the other division
    operators, *d*\=0 is accepted and following the rule it can be seen that n
    and c are considered congruent mod 0 only when exactly equal.


Exponentiation Functions
------------------------

.. function:: pow(base, exp)

    Return *base* raised to *exp*.

    *exp* is defined as `!int8` but must fit into a `!long` as defined on the
    server.

    The function is an alias for the `!^` operator.


.. function:: powm(base, exp, mod)

    Return (*base* raised to *exp*) modulo *mod*.

    Negative *exp* is supported if an inverse *base^-1* mod *mod* exists (see
    `invert()` function). If an inverse doesn't exist then a divide by zero is
    raised.


Root Extraction Functions
-------------------------

.. function:: root(op, n)

    Return the truncated integer part of the *n*\th root of *op*.

    *n* is defined as `!int8` but must fit into a `!long` as defined on the
    server.

.. function:: rootrem(op, n)

    Return a tuple of 2 elements with the truncated integer part of the *n*\th
    root of *op* and the remainder (*i.e.* *op* - *root* ^ *n*).

    .. code-block:: psql

        =# select * from rootrem(28, 3);
         root | rem
        ------+-----
         3    | 1

    .. note:: The function is not available on GMP version < 4.2.

.. function:: sqrt(op)

    Return the truncated integer part of the square root of *op*.

.. function:: sqrtrem(op)

    Return a tuple of 2 elements with the truncated integer part of the square
    root of *op* and the remainder (*i.e.* *op* - *root* \* *root*).

    .. code-block:: psql

        =# select * from sqrtrem(83);
         root | rem
        ------+-----
         9    | 2

.. function:: perfect_power(op)

    Return `!true` if *op* is a perfect power, *i.e.*, if there exist
    integers :math:`a` and :math:`b`, with :math:`b>1`, such that *op* equals
    :math:`a^b`.

    Under this definition both 0 and 1 are considered to be perfect powers.
    Negative values of op are accepted, but of course can only be odd perfect
    powers.

.. function:: perfect_square(op)

    Return `!true` if *op* is a perfect square, *i.e.*, if the square root of
    *op* is an integer. Under this definition both 0 and 1 are considered to
    be perfect squares.


Number Theoretic Functions
--------------------------

.. function:: probab_prime(n, reps)

    Determine whether *n* is prime. Return 2 if *n* is definitely prime,
    return 1 if *n* is probably prime (without being certain), or return 0 if
    *n* is definitely composite.

    This function does some trial divisions, then some `Miller-Rabin
    probabilistic primality tests`__. *reps* controls how many such tests are
    done, 5 to 10 is a reasonable number, more will reduce the chances of a
    composite being returned as "probably prime".

    .. __: https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test

    Miller-Rabin and similar tests can be more properly called compositeness
    tests. Numbers which fail are known to be composite but those which pass
    might be prime or might be composite. Only a few composites pass, hence
    those which pass are considered probably prime.

    .. seealso:: `Primality test
        <https://en.wikipedia.org/wiki/Primality_test>`__


.. function:: nextprime(op)

    Return the next prime greater than *op*.

    This function uses a probabilistic algorithm to identify primes. For
    practical purposes it's adequate, the chance of a composite passing will
    be extremely small.


.. function:: gcd(a, b)

    Return the greatest common divisor of *a* and *b*. The result is
    always positive even if one or both input operands are negative.


.. function:: gcdext(a, b)

    Return the greatest common divisor *g* of *a* and *b*, and in addition
    coefficients *s* and *t* satisfying :math:`a \cdot s + b \cdot t = g`. The
    value *g* is always positive, even if one or both of *a* and *b* are
    negative. The values *s* and *t* are chosen such that :math:`|s| \le |b|
    \hspace{0em}` and :math:`|t| \le |a| \hspace{0em}`.

    ..
        The \hspace{} are there to avoid the vim rest syntax highlighter to
        get crazy.

    .. code-block:: psql

        =# select * from  gcdext(6, 15);
         g | s  | t
        ---+----+---
         3 | -2 | 1


.. function:: lcm(a, b)

    Return the least common multiple of *a* and *b*. The value returned is
    always positive, irrespective of the signs of *a* and *b*. The return
    will be zero if either *a* or *b* is zero.


.. function:: fac(op)

    Return *op*\!, the factorial of *op*.


.. function:: bin(n, k)

    Return the `binomial coefficient`__ :math:`{n \choose k}`.
    Negative values of *n* are supported, using the identity
    :math:`{-n \choose k} = (-1)^k {n+k-1 \choose k}`.

    .. __: https://en.wikipedia.org/wiki/Binomial_coefficient


.. function:: fib(n)
              fib2(n)

    `!fib()` returns :math:`F_n`, the *n*\th `Fibonacci number`__.
    `!fib2()` returns :math:`F_n` and :math:`F_{n-1}`.

    .. __: https://en.wikipedia.org/wiki/Fibonacci_number

    These functions are designed for calculating isolated Fibonacci numbers.
    When a sequence of values is wanted it's best to start with `!fib2()`
    and iterate the defining :math:`F_{n+1}=F_n+F_{n-1}` or similar.


.. function:: lucnum(n)
              lucnum2(n)

    `!lucnum()` returns :math:`L_n`, the *n*\th `Lucas number`__.
    `!lucnum2()` returns :math:`L_n` and :math:`L_{n-1}`.

    .. __: https://en.wikipedia.org/wiki/Lucas_number

    These functions are designed for calculating isolated Lucas numbers.
    When a sequence of values is wanted it's best to start with `!lucnum2()`
    and iterate the defining :math:`L_{n+1}=L_n+L_{n-1}` or similar.

    The Fibonacci numbers and Lucas numbers are related sequences, so it's
    never necessary to call both `!fib2()` and `!lucnum2()`. The formulas for
    going from Fibonacci to Lucas can be found in `Lucas Numbers Algorithm`__,
    the reverse is straightforward too.

    .. __: https://gmplib.org/manual/Lucas-Numbers-Algorithm.html


.. function:: invert(a, b)

    Return the inverse of *a* modulo *b* if exists. The return value *r*
    will satisfy :math:`0 \le r \lt b`. If an inverse doesn't exist return
    `!NULL`.


.. function:: jacobi(a, b)

    Calculate the `Jacobi symbol`__ :math:`(\frac{a}{b})`. This is defined
    only for *b* odd.

    .. __: https://en.wikipedia.org/wiki/Jacobi_symbol


.. function:: legendre(a, p)

    Calculate the `Legendre symbol`__ :math:`(\frac{a}{p})`.  This is defined
    only for *p* an odd positive prime, and for such *p* it's identical to the
    Jacobi symbol.

    .. __: https://en.wikipedia.org/wiki/Legendre_symbol


.. function:: kronecker(a, b)

    Calculate the Jacobi symbol :math:`(\frac{a}{b})` with the Kronecker
    extension :math:`(\frac{a}{2})=(\frac{2}{a})` when *a* odd, or
    :math:`(\frac{a}{2})=0` when *a* even.

    .. seealso::
        Section 1.4.2, Henri Cohen, "A Course in Computational Algebraic
        Number Theory", Graduate Texts in Mathematics number 138,
        Springer-Verlag, 1993. https://www.math.u-bordeaux.fr/~cohen/


Logical and Bit Manipulation Functions
--------------------------------------

These functions behave as if twos complement arithmetic were used (although
sign-magnitude is the actual implementation). The least significant bit is
number 0.

.. table:: Logical Operators

    ======== ======================== =================================== ===================
    Operator Description              Example                             Return
    ======== ======================== =================================== ===================
    `!&`     Bitwise and              `!'0b10001'::mpz & '0b01001'::mpz`  `!'0b1'::mpz`
    `!|`     Bitwise inclusive-or     `!'0b10001'::mpz | '0b01001'::mpz`  `!'0b11001'::mpz`
    `!#`     Bitwise exclusive-or     `!'0b10001'::mpz # '0b01001'::mpz`  `!'0b11000'::mpz`
    ======== ======================== =================================== ===================


.. function:: com(op)

    Return the ones' complement of *op*.


.. function:: popcount(op)

    If op>=0, return the population count of *op*, which is the number of 1
    bits in the binary representation. If op<0, the number of 1s is infinite,
    and the return value is the largest possible, represented by
    `gmp_max_bitcnt()`.


.. function:: hamdist(op1, op2)

    If *op1* and *op2* are both >=0 or both <0, return the `Hamming
    distance`__
    between the two operands, which is the number of bit positions where *op1*
    and *op2* have different bit values. If one operand is >=0 and the other <0
    then the number of bits different is infinite, and the return value is the
    largest possible, represented by `gmp_max_bitcnt()`.

    .. __: https://en.wikipedia.org/wiki/Hamming_distance


.. function:: scan0(op, starting_bit)
              scan1(op, starting_bit)

    Scan *op*, starting from bit *starting_bit*, towards more significant
    bits, until the first 0 or 1 bit (respectively) is found. Return the index
    of the found bit.

    If the bit at *starting_bit* is already what's sought, then *starting_bit*
    is returned.

    If there's no bit found, then the largest possible bit count is returned
    (represented by `gmp_max_bitcnt()`). This will happen in `!scan0()` past
    the end of a negative number, or `!scan1()` past the end of a nonnegative
    number.


.. function:: setbit(op, bit_index)

    Return *op* with bit *bit_index* set.

.. function:: clrbit(op, bit_index)

    Return *op* with bit *bit_index* cleared.

.. function:: combit(op, bit_index)

    Return *op* with bit *bit_index* complemented.

    .. note:: The function is not available on GMP version < 4.2.

.. function:: tstbit(op, bit_index)

    Test bit *bit_index* in *op* and return 0 or 1 accordingly.


Random number functions
-----------------------

Sequences of pseudo-random numbers are generated using an internal per-session
variable, which holds an algorithm selection and a current state. Such a
variable must be initialized by a call to one of the `!randinit*()` functions,
and can be seeded with the `randseed()` function.

.. function:: randinit()

    Initialize the session random state with a default algorithm. This will be
    a compromise between speed and randomness, and is recommended for
    applications with no special requirements. Currently this is
    `randinit_mt()`.


.. function:: randinit_mt()

    Initialize the session random state for a `Mersenne Twister`__ algorithm.
    This algorithm is fast and has good randomness properties.

    .. __: https://en.wikipedia.org/wiki/Mersenne_twister

    .. note:: The function is not available on GMP version < 4.2.


.. function:: randinit_lc_2exp(a, c, e)

    Initialize the session random state with a `linear congruential`__
    algorithm :math:`X = (a \cdot X + c) \mod 2^e`.

    .. __: https://en.wikipedia.org/wiki/Linear_congruential_generator

    The low bits of *X* in this algorithm are not very random. The least
    significant bit will have a period no more than 2, and the second bit no
    more than 4, etc. For this reason only the high half of each *X* is
    actually used.

    When a random number of more than :math:`e/2` bits is to be generated,
    multiple iterations of the recurrence are used and the results
    concatenated.


.. function:: randinit_lc_2exp_size(s)

    Initialize the session random state for a linear congruential algorithm as
    per `randinit_lc_2exp()`. *a*, *c* and *e* are selected from a table,
    chosen so that size bits (or more) of each *X* will be used, ie.
    :math:`e/2 \ge s`.

    The function fails if *s* is bigger than the table data provides. The
    maximum size currently supported is 128.


.. function:: randseed(seed)

    Set an initial seed value into session random state.

    The size of a seed determines how many different sequences of random
    numbers is possible to generate. The "quality" of the seed is the
    randomness of a given seed compared to the previous seed used, and this
    affects the randomness of separate number sequences. The method for
    choosing a seed is critical if the generated numbers are to be used for
    important applications, such as generating cryptographic keys.

    Traditionally the system time has been used to seed, but care needs to be
    taken with this. If an application seeds often and the resolution of the
    system clock is low, then the same sequence of numbers might be repeated.
    Also, the system time is quite easy to guess, so if unpredictability is
    required then it should definitely not be the only source for the seed
    value. On some systems there's a special device ``/dev/random`` which
    provides random data better suited for use as a seed.


.. function:: urandomb(n)

    Generate a uniformly distributed random integer in the range :math:`0` to
    :math:`2^n-1`, inclusive.

    The session state must be initialized by calling one of the `!randinit()`
    functions before invoking this function.


.. function:: urandomm(n)

    Generate a uniformly distributed random integer in the range 0 to
    *n*\-1, inclusive.

    The session state must be initialized by calling one of the `!randinit()`
    functions before invoking this function.


.. function:: rrandomb(n)

    Generate a random integer with long strings of zeros and ones in the
    binary representation. Useful for testing functions and algorithms, since
    this kind of random numbers have proven to be more likely to trigger
    corner-case bugs. The random number will be in the range :math:`0` to
    :math:`2^n-1`, inclusive.

    The session state must be initialized by calling one of the `!randinit()`
    functions before invoking this function.


Aggregation functions
---------------------

.. function:: sum(z)

    Return the sum of *z* across all input values.

.. function:: prod(z)

    Return the product of *z* across all input values.

.. function:: max(z)

    Return the maximum value of *z* across all input values.

.. function:: min(z)

    Return the minimum value of *z* across all input values.

.. function:: bit_and(z)

    Return the bitwise and of *z* across all input values.

.. function:: bit_or(z)

    Return the bitwise inclusive-or of *z* across all input values.

.. function:: bit_xor(z)

    Return the bitwise exclusive-or of *z* across all input values.