GithubHelp home page GithubHelp logo

new RNG is very broken about chezscheme HOT 4 CLOSED

ocyzl avatar ocyzl commented on July 19, 2024
new RNG is very broken

from chezscheme.

Comments (4)

ocyzl avatar ocyzl commented on July 19, 2024 1

That looks much better. I think there are still a couple of small mistakes, though.

On line 3168, the second argument to random-int should be #x80000000 (231), not #x7FFFFFFF (231 - 1), because random-int returns a random number strictly less than its argument, and you want it to be able to return any 31-bit number, including the one that's all 1s.

Also, you can avoid some unnecessary calls to random-int if you let y be (- x 1) and then use y instead of x throughout the computation of maybe-result on lines 3160 - 3170. For example, if x is 231, you need 31 random bits, so it's sufficient to call random-int once, but the current code calls it at least twice (more if maybe-result turns out to be too big).

from chezscheme.

ocyzl avatar ocyzl commented on July 19, 2024 1

I'm not entirely sure I'm right, but here are my thoughts.

random-integer is supposed to return a number strictly less than x. So (- x 1) (i.e., y) is the largest number it could return. So (integer-length y) (i.e., len) is how many bits you need from random-int.

During the last iteration of the loop, when len < 32, the most significant bits of y (i.e., z) are the largest value that the corresponding bits of maybe-result should be allowed to have. And in order to get a random number that might be that large, you need to pass (+ z 1) to random-int, because it also returns a number strictly less than its argument.

So I think your current code is correct.

from chezscheme.

mflatt avatar mflatt commented on July 19, 2024

Thanks for the report!

from chezscheme.

mflatt avatar mflatt commented on July 19, 2024

Thank you for your analysis and help! I've pushed another attempt, and hopefully I understood you correctly about (- x 1). (I think I understand why that works, but I am not sure I have it right.)

from chezscheme.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.