]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libmp/port/mpnrand.c
merge
[plan9front.git] / sys / src / libmp / port / mpnrand.c
1 #include "os.h"
2 #include <mp.h>
3 #include "dat.h"
4
5 /* return uniform random [0..n-1] */
6 mpint*
7 mpnrand(mpint *n, void (*gen)(uchar*, int), mpint *b)
8 {
9         int bits;
10
11         bits = mpsignif(n);
12         if(bits == 0)
13                 abort();
14         if(b == nil){
15                 b = mpnew(bits);
16                 setmalloctag(b, getcallerpc(&n));
17         }
18         do {
19                 mprand(bits, gen, b);
20         } while(mpmagcmp(b, n) >= 0);
21
22         return b;
23 }