FastPrepXOR Pseudorandom Sequence

XOR Pseudorandom Sequence

Baseten logoBaseten● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Generate a deterministic sequence by updating a 32-bit state with XOR and shifts. For this exercise, use the following conventional xorshift32 practice recurrence. Begin with the nonzero unsigned state seed.

  1. state = (state XOR (state << 13)) modulo 2^32.
  2. state = state XOR (state >> 17), using a logical right shift.
  3. state = (state XOR (state << 5)) modulo 2^32.

After all three updates, emit the new unsigned state. Repeat for count consecutive updates and return those values in order. The initial seed itself is not an emitted value. A zero count returns an empty array.

The width, shifts and batch representation are practice conventions completing the reported XOR-based random-number-generator task. Complete xorRandomSequence.

Function

xorRandomSequence(seed: long, count: int) → long[]

Examples

Example 1

seed = 1count = 5return = [270369,67634689,2647435461,307599695,2398689233]

The first transition emits 270369; each following transition uses the previously emitted state.

Example 2

seed = 4294967295count = 2return = [253983,4228382207]

The seed has all 32 bits set. Outputs remain unsigned even when bit 31 is set.

Constraints

  • 1 <= seed <= 2^32 - 1.
  • 0 <= count <= 2000.
  • Keep the low 32 bits after each left-shift XOR. Right shift inserts zero bits.
  • Return unsigned values as nonnegative 64-bit integers in [0,2^32 - 1].

More Baseten problems

See Baseten hiring insights
public long[] xorRandomSequence(long seed, int count) {
    // Write your code here.
}
seed1
count5
expected[270369,67634689,2647435461,307599695,2398689233]
Checking account…