--- /n/sources/plan9/sys/include/mp.h Wed Jun 26 23:50:48 2024 +++ /sys/include/mp.h Wed Jun 26 23:59:55 2024 @@ -38,6 +38,8 @@ /* random bits */ mpint* mprand(int bits, void (*gen)(uchar*, int), mpint *b); +/* return uniform random [0..n-1] */ +mpint* mpnrand(mpint *n, void (*gen)(uchar*, int), mpint *b); /* conversion */ mpint* strtomp(char*, char**, int, mpint*); /* ascii */ --- /n/sources/plan9/sys/include/ape/mp.h Wed Jun 26 23:50:56 2024 +++ /sys/include/ape/mp.h Thu Jun 27 00:03:04 2024 @@ -47,6 +47,8 @@ /* random bits */ mpint* mprand(int bits, void (*gen)(uchar*, int), mpint *b); +/* return uniform random [0..n-1] */ +mpint* mpnrand(mpint *n, void (*gen)(uchar*, int), mpint *b); /* conversion */ mpint* strtomp(char*, char**, int, mpint*); /* ascii */ --- /n/sources/plan9/sys/man/2/mp Wed Jun 26 23:51:06 2024 +++ /sys/man/2/mp Wed Jun 26 23:59:55 2024 @@ -1,6 +1,6 @@ .TH MP 2 .SH NAME -mpsetminbits, mpnew, mpfree, mpbits, mpnorm, mpcopy, mpassign, mprand, strtomp, mpfmt,mptoa, betomp, mptobe, mptober, letomp, mptole, mptolel, mptoui, uitomp, mptoi, itomp, uvtomp, mptouv, vtomp, mptov, mpdigdiv, mpadd, mpsub, mpleft, mpright, mpmul, mpexp, mpmod, mpdiv, mpcmp, mpextendedgcd, mpinvert, mpsignif, mplowbits0, mpvecdigmuladd, mpvecdigmulsub, mpvecadd, mpvecsub, mpveccmp, mpvecmul, mpmagcmp, mpmagadd, mpmagsub, crtpre, crtin, crtout, crtprefree, crtresfree \- extended precision arithmetic +mpsetminbits, mpnew, mpfree, mpbits, mpnorm, mpcopy, mpassign, mprand, mpnrand, strtomp, mpfmt,mptoa, betomp, mptobe, mptober, letomp, mptole, mptolel, mptoui, uitomp, mptoi, itomp, uvtomp, mptouv, vtomp, mptov, mpdigdiv, mpadd, mpsub, mpleft, mpright, mpmul, mpexp, mpmod, mpdiv, mpcmp, mpextendedgcd, mpinvert, mpsignif, mplowbits0, mpvecdigmuladd, mpvecdigmulsub, mpvecadd, mpvecsub, mpveccmp, mpvecmul, mpmagcmp, mpmagadd, mpmagsub, crtpre, crtin, crtout, crtprefree, crtresfree \- extended precision arithmetic .SH SYNOPSIS .B #include .br @@ -34,6 +34,9 @@ mpint* mprand(int bits, void (*gen)(uchar*, int), mpint *b) .PP .B +mpint* mpnrand(mpint *n, void (*gen)(uchar*, int), mpint *b) +.PP +.B mpint* strtomp(char *buf, char **rptr, int base, mpint *b) .PP .B @@ -305,6 +308,14 @@ .I Gen takes a pointer to a string of uchar's and the number to fill in. +.PP +.I Mpnrand +uses +.I gen +to generate a uniform random number +.IR x , +.if t 0 ≤ \fIx\fR < \fIn\fR. +.if n 0 ≤ x < n. .PP .I Strtomp and diff -Nru /n/sources/plan9/sys/src/ape/lib/mp/port/mkfile /sys/src/ape/lib/mp/port/mkfile --- /n/sources/plan9/sys/src/ape/lib/mp/port/mkfile Wed Jun 26 23:42:10 2024 +++ /sys/src/ape/lib/mp/port/mkfile Thu Jun 27 00:04:08 2024 @@ -31,6 +31,7 @@ mpextendedgcd\ mpinvert\ mprand\ + mpnrand\ crt\ mptoi\ mptoui\ diff -Nru /n/sources/plan9/sys/src/libmp/port/mkfile /sys/src/libmp/port/mkfile --- /n/sources/plan9/sys/src/libmp/port/mkfile Wed Jun 26 23:40:10 2024 +++ /sys/src/libmp/port/mkfile Wed Jun 26 23:59:55 2024 @@ -29,6 +29,7 @@ mpextendedgcd\ mpinvert\ mprand\ + mpnrand\ crt\ mptoi\ mptoui\ diff -Nru /n/sources/plan9/sys/src/libmp/port/mpnrand.c /sys/src/libmp/port/mpnrand.c --- /n/sources/plan9/sys/src/libmp/port/mpnrand.c Wed Dec 31 19:00:00 1969 +++ /sys/src/libmp/port/mpnrand.c Wed Jun 26 23:59:58 2024 @@ -0,0 +1,24 @@ +#include "os.h" +#include +#include +#include "dat.h" + +/* return uniform random [0..n-1] */ +mpint* +mpnrand(mpint *n, void (*gen)(uchar*, int), mpint *b) +{ + int bits; + + bits = mpsignif(n); + if(bits == 0) + abort(); + if(b == nil){ + b = mpnew(bits); + setmalloctag(b, getcallerpc(&n)); + } + do { + mprand(bits, gen, b); + } while(mpmagcmp(b, n) >= 0); + + return b; +}