Sponsored Content
Full Discussion: mod on %RANDOM
Top Forums Shell Programming and Scripting mod on %RANDOM Post 302272025 by Perderabo on Monday 29th of December 2008 10:29:11 AM
Old 12-29-2008
When you use a modulus operation you are selecting information from the low order bits of a number and discarding information from the high order bits.

"these bits should be extracted from the most significant (left-hand) part
of the computer word, since the least significant bits produced by many random number generators are not sufficiently random."

and

"The least significant (right-hand) digits of X are not very random, so decisions based on the number X should always be influenced primarily by the most significant digits. It is generally best to think of X as a random fraction X/m between 0 and 1, that is, to visualize X with a decimal point at its left, rather than to regard X as a random integer between 0 and m - 1. To compute a random integer between 0 and k - 1, one should multiply by k and truncate the result."
both from The Art of Computer Programming, Volume 2 Seminumerical Algorithms by Donald Knuth

While neither is perfect, assuming that 0 <= RANDOM <= 32767,
((myrandom = RANDOM * 50 / 32768))
will behave better than
((myrandom = RANDOM % 50))
 

3 More Discussions You Might Find Interesting

1. Programming

Mod

I try to use mod(a,b) in a program but i cant Please help me (2 Replies)
Discussion started by: iwbasts
2 Replies

2. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

3. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies
BN_rand(3)							      OpenSSL								BN_rand(3)

NAME
BN_rand, BN_pseudo_rand - generate pseudo-random number SYNOPSIS
#include <openssl/bn.h> int BN_rand(BIGNUM *rnd, int bits, int top, int bottom); int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom); int BN_rand_range(BIGNUM *rnd, BIGNUM *range); int BN_pseudo_rand_range(BIGNUM *rnd, BIGNUM *range); DESCRIPTION
BN_rand() generates a cryptographically strong pseudo-random number of bits bits in length and stores it in rnd. If top is -1, the most significant bit of the random number can be zero. If top is 0, it is set to 1, and if top is 1, the two most significant bits of the number will be set to 1, so that the product of two such random numbers will always have 2*bits length. If bottom is true, the number will be odd. BN_pseudo_rand() does the same, but pseudo-random numbers generated by this function are not necessarily unpredictable. They can be used for non-cryptographic purposes and for certain purposes in cryptographic protocols, but usually not for key generation etc. BN_rand_range() generates a cryptographically strong pseudo-random number rnd in the range 0 <lt>= rnd < range. BN_pseudo_rand_range() does the same, but is based on BN_pseudo_rand(), and hence numbers generated by it are not necessarily unpredictable. The PRNG must be seeded prior to calling BN_rand() or BN_rand_range(). RETURN VALUES
The functions return 1 on success, 0 on error. The error codes can be obtained by ERR_get_error(3). SEE ALSO
bn(3), ERR_get_error(3), rand(3), RAND_add(3), RAND_bytes(3) HISTORY
BN_rand() is available in all versions of SSLeay and OpenSSL. BN_pseudo_rand() was added in OpenSSL 0.9.5. The top == -1 case and the function BN_rand_range() were added in OpenSSL 0.9.6a. BN_pseudo_rand_range() was added in OpenSSL 0.9.6c. 1.0.1e 2013-02-11 BN_rand(3)
All times are GMT -4. The time now is 08:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy