V
V
vasIvas2015-10-23 21:35:15
JavaScript
vasIvas, 2015-10-23 21:35:15

Why doesn't math work in js?

All in all, this is the thing. There is a piece of code written in as3 and its analogue in haxe.
I needed to use it in js, so I rewrote it, but it doesn't work.
Then I took haxe and compiled it into js, ​​but the problem remained the same.

public static function getInt(min:Int, max:Int):Int {
        seed = 214013 * seed + 2531011;
        return min+(seed ^ (seed>>15))%(max-min+1);
}

function getInt(min, max) {
        seed = 214013 * seed + 2531011;
        return min+(seed ^ (seed>>15))%(max-min+1);
}

Why doesn't the math work?
UPD:
the code does not work because out of ten iterations, six are always minimal, and six are the last.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aves, 2015-10-23
@vasIvas

The bit operators make any number a 32-bit integer. When seed becomes greater than 4294967295, then 0 is used instead.
upd seed ^ (seed >> 15) becomes always 0.
upd2 when seed > 2 32 seed >> 15 is always 0, when seed > 2 54 seed ^ 0 is always 0
upd3 after experimenting, I found out that it is essentially correct, but inaccurate, numbers greater than 2 32 + 53 binary operators turn into 0

V
Vladislav Startsev, 2015-10-23
@esvlad

What is the seed variable, or rather what type is it, you can try to wrap it in Integer
parseInt(seed);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question