How to get the nth number in the rand () sequence directly without calling rand () n times?

According to my understanding, setting srand with a specific seed calls a sequence of calls to rand () to get the same series of numbers every time for that particular seed:

For instance:

             srand(seed1);
             rand() // firstnumber (e.g.: 42)
             rand() // second number (e.g: 17)
             srand(seed1)
             rand() // first number (same as above (42))
             rand() // second number (same as above (17))

Is there a way to get the nth number in a sequence directly without calling rand () n times?

  • For example, if I want the 17th random number in a series, I want to get the number in one call, instead of calling rand () 17 times.

  • I cannot precompile and save values

EDIT: I watched this article:

https://mathoverflow.net/questions/104915/pseudo-random-algorithm-allowing-o1-computation-of-nth-element

, , , , , , .

EDIT: , "" n- , , rand , . , , , , . .

EDIT: PRNG. , , , . , . .

  • , .
  • , .
  • n- O (1) .

: - . n- O (1) . n- , , n- O (1)

+4
7

rand . , PRNG. , PRNG.

class Rand {
    int seed;
    const int a = 1103515245;
    const int c = 12345;
public:
    Rand();
    void srand( int );
    int rand();
};

Rand::Rand() : seed(123456789) {}

void Rand::srand( int s ) { seed = s; }

int Rand::rand()
{
  seed = a * seed + c;
  return seed;
}

OP : " rand ". Rand . Rand , .

+2

rand_r(). . , , . , .


( ) unsigned int. ; , init; , +initialize. , , /dev/random . , - , ( ).

, , rand_r(&yourSeedVariable). , , - . , rand(). , .

. rand_r() , . rand().


. :

, "" n- , , rand , . , , , , .

. , . * n * th . , .

+2

++ 11 PRNG discard ",

#include <random>
#include <iostream>

int main() {
    std::mt19937 rng;
    static const size_t distance = 5;

    rng.seed(0);
    rng.discard(distance);
    std::cout << "after discard 5: " << rng() << '\n';

    rng.seed(0);
    for (size_t i = 0; i <= distance; ++i) {
        std::cout << i << ": " << rng() << '\n';
    }
}

http://ideone.com/0zeRNq

after discard 5: 3684848379
0: 2357136044
1: 2546248239
2: 3071714933
3: 3626093760
4: 2588848963
5: 3684848379
+2

, PRNG, , ( , ) . , N- N-1.

0

. , std::rand() - (CTR) . , . - , . , .

, 8675309 8008135. , 8008136, 8008137, 8008138, 8008139, 8008140... . 17- , (8008135 + 17) = 8008152.

0

1:1 - 32- 64- . , PRNG / , xorshift :

uint64_t state;

void srand(uint64_t seed) {
  state = seed;
}

uint64_t hash(uint64_t x) {
  x ^= x >> 12;
  x ^= x << 25;
  x ^= x >> 27;
  return x * 2685821657736338717ull;
}

uint32_t rand(void) {
  return hash(state++) >> 32;
}

uint32_t rand(uint32_t n) {
  return hash(n) >> 32;
}
0

: .

Longer answer: pseudorandom series are β€œrandom” in that the computer cannot pre-calculate the series without knowing the pre-calculated element (or seed), but β€œpseudo” in that the series is reproducible using the same seeds.

Of using Google-fu, LSFR requires a finite number of states. The PRNG you are trying to get, do not do this.

-1
source

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


All Articles