Retired

Can you predict Math.random()? (retired)

Description
Loading description...
Puzzles
Mathematics
Algorithms
Logic
Numbers
  • Please sign in or sign up to leave a comment.
  • Voile Avatar

    Also, because of how you sequence the actions in the test cases nasty hacks like this solution is possible:

    https://www.codewars.com/kata/reviews/582ff32539f6540e78000033/groups/596dba1509eecd8336000a46

  • Voile Avatar

    The kata author is not even trying.

  • __TomFoolery__ Avatar

    Its very simle, test case passes, random test case passes, submission throws error, TypeError: random is not a function at Object.handleError at ContextifyScript.Script.runInThisContext at Object.exports.runInThisContext

    still passes and gives honour  just dont submit
    
  • kazk Avatar

    "Hacking the JavaScript Lottery" was interesting read. Thanks for bringing it up :)

    This kata looks really time consuming, so I'd like to make sure if I'm understanding correctly before starting.

    In order to solve this kata, we have to:

    1. Read src/base/utils/random-number-generator.{cc,h} and figure out how to recover random bits from random double
    2. Implement some 64-bit unsigned integer arithmetic (^,<<,>>>,&,+)
    3. Implement XorShift128+ and NextDouble()
    4. Implement SMT solver for bitvectors
    5. Implement symbolic XorShift128+ with constraint comparing lower bits of symbolically generated and the actual
    6. Solve for unknown state0 and state1 using lower 52 bits from consecutive outputs of Math.random
    7. Define random as a function generating double from XorShift128+ with state0 and state1

    Is this correct interpretation?

    Also, how are you ensuring that Math.random is not called anywhere else?

    • If we are defining random using shared Math.random, how are you ensuring that nothing else calls it? I mean what if the Codewars test framework or some module used by the test framework uses Math.random?
    • If we are defining random in completely isolated context (as in vm.createContext), how are you ensuring Math.random in Test.assertEquals(random(), Math.random()); to equal Math.random that was used?

    edit: reworded & cleaned up

    • kazk Avatar

      Never mind. I missed the comment by @Testerinhio for some reason :|

      Question marked resolved by kazk 9 years ago
    • gkucmierz Avatar

      But you asked very good question. Probably tests are not using Math.random() but as I cant be sure about that, I changed testing code to avoid problem you described.

    • kazk Avatar

      I just skimmed through the new example test.

      First, this doesn't call random at all because runTests is 0.

        let runTests = 0;
        let correct = true;
        
        for (let i = 0; i < runTests; ++i) {
      

      Second, I don't think this changes anything. Placing tests inside IIFE does not avoid the problem I described. Math.random inside it still references the same global Math.random. IIFE creates new evaluation scope within the same execution context.

      Math.random == vm.runInContext('Math.random', vm.createContext()); //=> false
      Math.random == (function() { return Math.random; })(); //=> true
      
  • Testerinhio Avatar

    @gkucmierz: You have not solved it yourself?!?! You only have your hack to get this kata published?

    • gkucmierz Avatar

      Hey, is it illegal? I have no time to do this now, but I know it is possible and it is very interesting kata in my opinion.

      BTW. how you can see my sources? I cant see your solution.

  • donaldsebleung Avatar

    This comment has been hidden.