Shop OBEX P1 Docs P2 Docs Learn Events
(Noobie) Operator ?? Generates Random Number XOR032 — Parallax Forums

(Noobie) Operator ?? Generates Random Number XOR032

Using following code:

x := 3
y := ??x
debug(udec(y),udec(x))

It Generates in debug window :
cog 0 y= 658_179_587 x= 2_377_223_183

Why isn't y == x (bit of pun y y)

Regards
WRD Bob Drury

Comments

  • In this case you are using x as the state register for the PseudoRandom Number Generator (PRNG), and capturing the output in y.

    You are seeding the state register with 3 and then asking the PRNG to iterate using that seed. The outcome of this is to provide the 'random' number in y and to update the state register x.

    If these were the same then you wouldn't need both.

  • My interpretation is that any time the ?? operator is used the variable it works on should never be used unless generating another random number because it is
    maintaining a number to be used to generate another Random number. if X is redefined to the same number 3 the same number would be generated. But it still seems a little
    confusing because the number 2_377_223_183 or 658_179_587 would generate a different number than 3 would.
    Thanks AJL for reply
    WRD Bob Drury

  • You're right.

    The PRNG generates two numbers at every state: the output and the next state, but the states don't progress as a linear count.
    A seed state of 3 generates an output of 658_179_587. It also advances the state to 2_377_223_183.
    The next step would use the state of 2_377_223_183 to generate the next output, and the next state.

    The sequence from a given seed is absolutely deterministic, but appears random.
    If the states progressed in linear fashion then someone could reverse engineering the generator algorithm from a very small data set. The way that most good PRNGs work is to make the state progression appear random too, making it much harder.

    In short, x is your state storage and after seeding you shouldn't worry too much about it; y is your output and you should simply grab the next value in the sequence and use it.

  • evanhevanh Posts: 16,023
    edited 2021-03-04 09:01

    It's like a shuffled deck of cards, 4 G cards in the deck. Each time you use it with that x variable it'll iterate to the next card - pick the next card off the deck.

    The catch is the order of the shuffle is predetermined and fixed in the hardware. Only option for apparent different sequence is to change the initial seed value of x. This is like cutting the deck and putting the top portion at the bottom. A predetermined sequence is often a desirable feature for providing a random effect that is repeatable. If you want an undetermined sequence then use getrnd() instead. getrnd() can be a way to seed x too.

    A couple of technical details of the Xoroshiro32 implementation used in the prop2:

    • The output value is separated from the state value because the state engine generates undesirable artefacts in random theory. The output is from a hash like function, called the scrambler, applied to the iterated state variable to smooth away those artefacts. PS: This is a normal part of the published Xoroshiro algorithm, although the XORO32 implementation has a modified scrambler.
    • Each XORO32 instruction actually double iterates and picks two consecutive cards from the deck. The reason for this is due to the general nature of the Xoroshiro algorithm. It has a convenient 2:1 relationship between the state size of x, in this case 32-bit, and the output data width, which would be 16-bit. But since we want a 32-bit output it's easy to simply join two consecutive outputs together.

    Further reading - https://forums.parallax.com/discussion/168188/xoroshiro-random-number-generator/p1

    .

  • THANKS ajl AND evanh
    Generating something truly random not easy.
    Regards
    WRD Bob Drury

Sign In or Register to comment.