t_bdQuickRandBits.c (original) (raw)
`/* Id:tbdQuickRandBits.cId: t_bdQuickRandBits.c Id:tbdQuickRandBits.c */
/*
- Copyright (C) 2001-26 David Ireland, D.I. Management Services Pty Limited
- https://di-mgt.com.au/contact/ https://di-mgt.com.au/bigdigits.html
- SPDX-License-Identifier: MPL-2.0
- Last updated:
- Date:2026−03−2905:11:00Date: 2026-03-29 05:11:00 Date:2026−03−2905:11:00
- Revision:2.7.0Revision: 2.7.0 Revision:2.7.0
- Author:daiAuthor: dai Author:dai */
/* What is the difference between bdQuickRandBits and bdRandomBits?
bdRandomBits() uses the "pretty-good" internal RNG and requires you to include bigdRand.h and compile with bigdRand.c and bigdigitsRand.c. You can be pretty sure that the numbers generated will pass all statistical tests for randomness.
bdQuickRandBits() just needs the standard bigd.h include file and you do not need to add the modules bigdRand.c and bigdigitsRand.c to your project. It uses the stdlib rand() function, which is by no means secure, but it will quickly fill up a number with the required number of random-looking bits. It might repeat if you call twice in quick succession. It's quick and dirty, as advertised.
If you really want a secure random number, then use bdRandomSeeded() and call your own super-secure random number generator, throwing in your own bit of extra entropy with the seed.
*/
#include <stdio.h> #include <assert.h> #include "bigd.h" #include "bigdRand.h"
int main(void) { BIGD a; int i;
a = bdNew();
// Test bdQuickRandBits (quick-and-dirty)
printf("Testing bdQuickRandBits...\n");
for (i = 0; i < 10; i++)
{
bdQuickRandBits(a, 128);
bdPrintHex("", a, "\n");
}
// Test bdRandomBits (more secure)
printf("Testing bdRandomBits...\n");
for (i = 0; i < 10; i++)
{
bdRandomBits(a, 128);
bdPrintHex("", a, "\n");
}
return 0;}`