Random number generator (device): Difference between revisions

Content added Content deleted
No edit summary
Line 37: Line 37:
if (r == NULL)
if (r == NULL)
{
{
perror("/dev/urandom");
perror("/dev/urandom");
return 1;
return 1;
}
}


Line 44: Line 44:
if (br < 1)
if (br < 1)
{
{
fputs("/dev/urandom: Not enough bytes\n", stderr);
fputs("/dev/urandom: Not enough bytes\n", stderr);
return 1;
return 1;
}
}


Line 56: Line 56:
[http://www.openbsd.org/cgi-bin/man.cgi?query=arc4random&apropos=0&sektion=3&manpath=OpenBSD+Current&arch=i386&format=html arc4random()] appeared in [[OpenBSD]] 2.1 and has spread to many [[BSD]] systems. This function runs an ARC4 random number generator that takes entropy from a kernel device. (This kernel device is sysctl kern.arandom in OpenBSD, or /dev/urandom in some other systems.)
[http://www.openbsd.org/cgi-bin/man.cgi?query=arc4random&apropos=0&sektion=3&manpath=OpenBSD+Current&arch=i386&format=html arc4random()] appeared in [[OpenBSD]] 2.1 and has spread to many [[BSD]] systems. This function runs an ARC4 random number generator that takes entropy from a kernel device. (This kernel device is sysctl kern.arandom in OpenBSD, or /dev/urandom in some other systems.)


<lang c>#include <inttypes.h> /* PRIu32 */
<lang c>#include <inttypes.h> /* PRIu32 */
#include <stdlib.h> /* arc4random */
#include <stdlib.h> /* arc4random */
#include <stdio.h> /* printf */
#include <stdio.h> /* printf */


int
int
main()
main()
{
{
printf("%" PRIu32 "\n", arc4random());
printf("%" PRIu32 "\n", arc4random());
return 0;
return 0;
}</lang>
}</lang>


Line 79: Line 79:
main()
main()
{
{
uint32_t v;
uint32_t v;


if (RAND_bytes((unsigned char *)&v, sizeof v) == 0) {
if (RAND_bytes((unsigned char *)&v, sizeof v) == 0) {
ERR_print_errors_fp(stderr);
ERR_print_errors_fp(stderr);
return 1;
return 1;
}
}
printf("%" PRIu32 "\n", v);
printf("%" PRIu32 "\n", v);
return 0;
return 0;
}</lang>
}</lang>


Line 92: Line 92:
{{works with|MinGW}}
{{works with|MinGW}}


<lang c>#include <stdio.h> /* printf */
<lang c>#include <stdio.h> /* printf */
#include <windows.h>
#include <windows.h>
#include <wincrypt.h> /* CryptAcquireContext, CryptGenRandom */
#include <wincrypt.h> /* CryptAcquireContext, CryptGenRandom */


int
int
main()
main()
{
{
HCRYPTPROV p;
HCRYPTPROV p;
ULONG i;
ULONG i;


if (CryptAcquireContext(&p, NULL, NULL,
if (CryptAcquireContext(&p, NULL, NULL,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) {
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) {
fputs("CryptAcquireContext failed.\n", stderr);
fputs("CryptAcquireContext failed.\n", stderr);
return 1;
return 1;
}
}
if (CryptGenRandom(p, sizeof i, (BYTE *)&i) == FALSE) {
if (CryptGenRandom(p, sizeof i, (BYTE *)&i) == FALSE) {
fputs("CryptGenRandom failed.\n", stderr);
fputs("CryptGenRandom failed.\n", stderr);
return 1;
return 1;
}
}
printf("%lu\n", i);
printf("%lu\n", i);
CryptReleaseContext(p, 0);
CryptReleaseContext(p, 0);
return 0;
return 0;
}</lang>
}</lang>


Line 226: Line 226:


public class RandomExample {
public class RandomExample {
public static void main(String[] args) {
public static void main(String[] args) {
SecureRandom rng = new SecureRandom();
SecureRandom rng = new SecureRandom();


/* Prints a random signed 32-bit integer. */
/* Prints a random signed 32-bit integer. */
System.out.println(rng.nextInt());
System.out.println(rng.nextInt());
}
}
}</lang>
}</lang>


Line 245: Line 245:
bytes <- getEntropy 4
bytes <- getEntropy 4
print (runGet getWord32be $ B.fromChunks [bytes])</lang>
print (runGet getWord32be $ B.fromChunks [bytes])</lang>

=={{header|Lasso}}==
<lang lasso>file(`/dev/urandom`)->readSomeBytes(4)->export32bits</lang>
{{out}}
<pre>723217350</pre>


=={{header|NetRexx}}==
=={{header|NetRexx}}==