Quickly generate random numbers that seem random

I am looking for an effective way to generate numbers that a person would perceive as random. Basically, I think about it, avoiding long sequences of 0 or 1 bit. I expect people to view the bitmap, and a processor with a very low power should calculate about a thousand of them per second.

There are two different concepts that I can come up with, but I have lost the opportunity to find an effective way to achieve them.

  • Create a random number with a fixed number of one bit. For a 32-bit random number, this requires up to 31 random numbers using the Whip selection algorithm. Is there a more efficient way to generate a random number with some bits? Unfortunately, 0000FFFF does not look very random.

  • Some form of โ€œpartialโ€ density seems to look better โ€” but I can't think of a clear way to do this โ€” I would think through each piece and figure out how far from the ideal density and try to increase the bit density of the next fragment. That sounds complicated.

Hopefully there is another algorithm that I have not thought about. Thanks in advance for your help.

[EDIT] I must clarify what I ask -
(a) Is there an efficient way to generate random numbers without โ€œlongโ€ runs of one bit, where โ€œlongโ€ is a custom parameter?
(b) Other suggestions on what would make the number less random?

+3
source share
12

A , , , .

: , , , . question.

+6

, , "" . - , ? , , , . , , , , . , 32- , , , 0 1, ?

, .

random_number.png

+2

, , - " ". , " " , , , , .

"" , , "" ( ), .

+1

. , 00101011100101100110100101100101, .

, ? ? 0 1.

, , .

+1

1 0, , , , , . , , , , . 32 "" , 8 .

, , .

, 1 , , 1

int a = 0x00FF;

twiddling hack, .

+1

, - :

#include <cstdlib>

class generator {
public:
   generator() : last_num(0), run_count(1) { }

   bool next_bit() {
      const bool flip = rand() > RAND_MAX / pow( 2, run_count);
                               // RAND_MAX >> run_count ? 
      if(flip) {
         run_count = 1;
         last_num = !last_num;
      } else
         ++run_count;

      return last_num;
   }
private:
   bool last_num;
   int run_count;
};

, . RAND_MAX/1 + run_count,

+1

, , , .

- , , 4 ( n/(2 ^ (n-1))) 1. (, , , ):

0111111011111110110001000101111001100000000111001010101101001000

8. , 8 256 , 64 .

" " - , , . :

loop
    get a random number
    output that many 1 bits
    get a random number
    output that many 0 bits
endloop

, , , , 1. , N- 1, , " ", -, ", " , 50%, .

, " " :

get a uniformly-distributed random number n from 1 to 81
if n is between 1 and 54, return 1
if n is between 55 and 72, return 2
if n is between 72 and 78, return 3
if n is between 79 and 80, return 4
return 5

, N N-1 . 5, " " . , "" , , , . , N N-1.

, , log (81) = 6,34 " ", 1,44 , , . , 7/1,44 = 5 , LFSR .

+1

:

const int max_repeated_bits = 4;  /* or any other number that you prefer */

int examine_1(unsigned int x) {      
  for (int i=0; i<max_repeated_bits; ++i) x &= (x << 1);
  return x == 0;
}

int examine(unsigned int x) {
  return examine_1(x) && examine_1(~x);
}

x, (x) 0, . 32- 4 2/3, 3 . , 4 , . , 6 20%, 1,25 .

0

, shrinking self-shrinking, LFSR .

, 0,5, , 0,25.

LFSR , - LFSR . , . , , .

, - N , . , , - .

0

GSL. , , , . , , . , , .

0

, : () 2N:

PeopleRandom()
{
    while(1)
    {
        Number = randomN_bitNumber();
        if(Number && Number != MaxN_BitNumber)
            return Number;
    }
}

, 32- .. rand

:

  • 2/2 ^ N .
  • N .

, 1 , , N, , .

0

, , . 0,5, , ( ). p = 0 1010101010101010; p = 1 0s 1s.

# :

double p = 0.3; // 0 <= p <= 1, probability of duplicating a bit

var r = new Random();
int bit = r.Next(2);    

for (int i = 0; i < 100; i++)
{
    if (r.NextDouble() > p)
    {
        bit = (bit + 1) % 2;
    }        

    Console.Write(bit);                
}

This may be too slow for your needs, since you need to create a random double to get every new random bit. Instead, you can generate a random byte and use each pair of bits to generate a new bit (i.e., if both are zero, then keep the same bit, otherwise flip it if you are happy with the equivalent of a fixed p = 0.25).

In addition, you can still get long sequences of repeated bits, you just reduced the likelihood of this.

0
source

Source: https://habr.com/ru/post/1711220/


All Articles