#πŸ”’ Mimic Python's Random choices in JavaScript

39 messages Β· Page 1 of 1 (latest)

shrewd charm
#

Hey! I'm not sure if it stays Python related but here's a simple short script I made to make choices based on a seed in Python:

import random


if __name__ == "__main__":
    seed: str = "ARandomSeed"
    results: int = 3
    random.seed(seed)
    fields = list(range(25))
    res = []
    for _ in range(results):
        field = random.choice(fields)
        res.append(field)
        fields.remove(field)
    res.sort()
    print(res)

The thing I'd like to do is reproduce the random choices in JavaScript, I'd like the two different program to output the exact same results if they have the same seed!

forest sparrowBOT
#

@shrewd charm

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ionic heath
#

then you don't have to remove stuff

#

!e ```py
import random

if name == "main":
seed = "ARandomSeed"
results = 3
random.seed(seed)
res = sorted(random.sample(range(25), k=results))
print(res)

forest sparrowBOT
shrewd charm
#

!e

import random


if __name__ == "__main__":
    seed: str = "ARandomSeed"
    results: int = 3
    random.seed(seed)
    fields = list(range(25))
    res = []
    for _ in range(results):
        field = random.choice(fields)
        res.append(field)
        fields.remove(field)
    res.sort()
    print(res)
forest sparrowBOT
ionic heath
#

ah wait

shrewd charm
#

Output aren't the same if I use random.sample!

ionic heath
#

interesting

#

so you gotta look in the implementation

#

all the python source code is on github

twin meadow
twin meadow
shrewd charm
#

I already tried something:

    class MersenneTwister {
        constructor() {
            this.N = 624;
            this.M = 397;
            this.MATRIX_A = 0x9908b0df;
            this.UPPER_MASK = 0x80000000;
            this.LOWER_MASK = 0x7fffffff;

            this.mt = new Array(this.N);
            this.mti = this.N + 1;
        }

        init_genrand(s) {
            this.mt[0] = s >>> 0;
            for (this.mti = 1; this.mti < this.N; this.mti++) {
                let s = this.mt[this.mti - 1] ^ (this.mt[this.mti - 1] >>> 30);
                this.mt[this.mti] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) +
                    (s & 0x0000ffff) * 1812433253) + this.mti;
                this.mt[this.mti] >>>= 0;
            }
        }

        init_by_array(init_key) {
            let key_length = init_key.length;
            this.init_genrand(19650218);
            let i = 1;
            let j = 0;
            let k = this.N > key_length ? this.N : key_length;
            for (; k; k--) {
                let s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30);
                this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) +
                    ((s & 0x0000ffff) * 1664525))) + init_key[j] + j;
                this.mt[i] >>>= 0;
                i++;
                j++;
                if (i >= this.N) { this.mt[0] = this.mt[this.N - 1]; i = 1; }
                if (j >= key_length) { j = 0; }
            }
            for (k = this.N - 1; k; k--) {
                let s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30);
                this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) +
                    ((s & 0x0000ffff) * 1566083941))) - i;
                this.mt[i] >>>= 0;
                i++;
                if (i >= this.N) { this.mt[0] = this.mt[this.N - 1]; i = 1; }
            }
            this.mt[0] = 0x80000000;
        }

        genrand_int32() {
            let y;
            let mag01 = [0x0, this.MATRIX_A];

            if (this.mti >= this.N) {
                let kk;

                if (this.mti === this.N + 1)
                    this.init_genrand(5489);

                for (kk = 0; kk < this.N - this.M; kk++) {
                    y = (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK);
                    this.mt[kk] = this.mt[kk + this.M] ^ (y >>> 1) ^ mag01[y & 0x1];
                }
                for (; kk < this.N - 1; kk++) {
                    y = (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK);
                    this.mt[kk] = this.mt[kk + (this.M - this.N)] ^ (y >>> 1) ^ mag01[y & 0x1];
                }
                y = (this.mt[this.N - 1] & this.UPPER_MASK) | (this.mt[0] & this.LOWER_MASK);
                this.mt[this.N - 1] = this.mt[this.M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];

                this.mti = 0;
            }

            y = this.mt[this.mti++];

            /* Tempering */
            y ^= y >>> 11;
            y ^= (y << 7) & 0x9d2c5680;
            y ^= (y << 15) & 0xefc60000;
            y ^= y >>> 18;

            return y >>> 0;
        }

        random() {
            let a = this.genrand_int32() >>> 5; // Upper 27 bits
            let b = this.genrand_int32() >>> 6; // Upper 26 bits
            return ((a * 67108864.0) + b) * (1.0 / 9007199254740992.0);
        }
    }
#

But it's not working

#

I mean, it is

#

But it's not giving the same output as Python

twin meadow
#

they're not the same?

#

ah

ionic heath
#

therefore it returns unique elements

twin meadow
#

oh oops I didn't notice that

ionic heath
#

i think it's due to the length changing

#

whereas in sample you don't change the length

twin meadow
#

yeah probably

#

anyway my guess is that there is some implementation detail which is different from cpython's algorithm and yours

#

the easiest way to get the same random numbers would probably be to talk to a python server from your javascript and return some random numbers, unless you want to try and debug your pseudo-random number generator 😒

shrewd charm
#

OR

#

Maybe there’s an API where I could send a python code eval

#

And use the output in my JavaScript code

twin meadow
#

you could also try that

#

this seems like an interesting problem so I might try and work on it later when I get back from walking my dog (if I remember)

twin meadow
#

I was also thinking about that πŸ˜‚πŸ˜‚

forest sparrowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.