<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>137396</bug_id>
          
          <creation_ts>2014-10-03 12:19:32 -0700</creation_ts>
          <short_desc>Bias in first few random numbers generated by WeakRandom</short_desc>
          <delta_ts>2016-06-16 01:22:02 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>Mac (Intel)</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter>ajith</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ap</cc>
    
    <cc>bastien</cc>
    
    <cc>ggaren</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1039289</commentid>
    <comment_count>0</comment_count>
    <who name="">ajith</who>
    <bug_when>2014-10-03 12:19:32 -0700</bug_when>
    <thetext>The first few random numbers generated after WeakRandom is initialized show a bias. While this is may not be an issue for a web-page calling Math.random many more times (the random stream has good uniformity properties), it is a problem in the following scenario. 

Consider a web-app that runs experiments and shows different content dynamically based on 2 successive random numbers returned by Math.random.  The first number decides if that page load is eligible for the experiment or not; if eligible, the second number decides on showing one of two content variations with 50-50 probability. The attached file &apos;RandomBucketingBias.cpp&apos; simulates these bucketing trials 100,000 times, initializing WeakRandom at each iteration to simulate a distributed scenario where many different end-users load the page independently.

Compile &apos;RandomBucketingBias&apos;: g++ -o RandomBucketingBias RandomBucketingBias.cpp
Running &apos;RandomBucketingBias&apos;: ./RandomBucketingBias
shows something like:
Running bucketing tests:
Of 100000 tests, 50004 ignored, 49996 not ignored
Bucket counts 31235 18761, ratio = 1.664890

PASS: Ignores trials as expected
FAIL: First bucket count as expected
FAIL: Second bucket count as expected

One can skip the first k random numbers after initializing WeakRandom. Interestingly, skipping 3 numbers seems to get rid of the bias.
RandomBucketingBias takes 2 arguments. 
The first is the ignore percentage, an integer between 0 and 100 to specify what percentage of the trials must proceed to the second bucketing stage.
The second is the number of random numbers to skip before running the experiment. So, ./RandomBucketingBias 50 3 shows:
Running bucketing tests:
Of 100000 tests, 50020 ignored, 49980 not ignored
Bucket counts 24841 25139, ratio = 0.988146

PASS: Ignores trials as expected
PASS: First bucket count as expected
PASS: Second bucket count as expected

I also tested this against the Chromium RNG code; it shows no bias. For the full test see:  https://gist.github.com/ajithopti/bdd3cb21c89e203f569a</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1039291</commentid>
    <comment_count>1</comment_count>
      <attachid>239219</attachid>
    <who name="">ajith</who>
    <bug_when>2014-10-03 12:20:46 -0700</bug_when>
    <thetext>Created attachment 239219
C++ code to reproduce bug.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1041965</commentid>
    <comment_count>2</comment_count>
    <who name="">ajith</who>
    <bug_when>2014-10-15 14:20:38 -0700</bug_when>
    <thetext>Any progress on this? There&apos;s a blog post in the works about this, so in the ideal case, am hoping for a fix before that is published.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1042271</commentid>
    <comment_count>3</comment_count>
    <who name="Geoffrey Garen">ggaren</who>
    <bug_when>2014-10-16 19:18:19 -0700</bug_when>
    <thetext>Looks like initializeSeed can correct for bias by using a different constant.

Can your experiment work around this limitation by explicitly skipping the first few random numbers?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1042846</commentid>
    <comment_count>4</comment_count>
    <who name="">ajith</who>
    <bug_when>2014-10-20 09:56:39 -0700</bug_when>
    <thetext>We currently have a fix that avoids the bucketing issues. If a different constant does indeed fixes this issue, it&apos;d be great to have this fix in. Thanks for checking this.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1047098</commentid>
    <comment_count>5</comment_count>
    <who name="">ajith</who>
    <bug_when>2014-11-07 09:43:00 -0800</bug_when>
    <thetext>Geoffrey,
Just checking. Will you or someone else be applying a fix for this?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1202165</commentid>
    <comment_count>6</comment_count>
    <who name="Bastien Caudan">bastien</who>
    <bug_when>2016-06-14 06:46:45 -0700</bug_when>
    <thetext>It seems that we have this particular issue with our a/b test algorithm. We have this kind of repartition between test versions on safari:
Version 0:    800 
Version 1: 11 000  
Version 2: 35 000 
Version 3: 24 000 

We observe this behavior with all safari versions with significant traffic: 9.x, 8.0, 7.0, 6.1.6
However, Safari 4.0 does not seem to have this issue. 

We have reproduced this behavior on all safari versions available on saucelabs with this piece of code : https://gist.github.com/bcaudan/bfab026fa2416af9fc48dfacc4a59938

Do you have any news on this issue?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1202227</commentid>
    <comment_count>7</comment_count>
    <who name="Geoffrey Garen">ggaren</who>
    <bug_when>2016-06-14 10:50:00 -0700</bug_when>
    <thetext>Have you tested Safari Technology Preview?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1202544</commentid>
    <comment_count>8</comment_count>
    <who name="Bastien Caudan">bastien</who>
    <bug_when>2016-06-15 05:54:07 -0700</bug_when>
    <thetext>No. Is it supposed to be fixed?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1202657</commentid>
    <comment_count>9</comment_count>
    <who name="Geoffrey Garen">ggaren</who>
    <bug_when>2016-06-15 12:36:59 -0700</bug_when>
    <thetext>Yes.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1202891</commentid>
    <comment_count>10</comment_count>
    <who name="Bastien Caudan">bastien</who>
    <bug_when>2016-06-16 01:22:02 -0700</bug_when>
    <thetext>Great, then! Thx.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>239219</attachid>
            <date>2014-10-03 12:20:46 -0700</date>
            <delta_ts>2014-10-03 12:20:46 -0700</delta_ts>
            <desc>C++ code to reproduce bug.</desc>
            <filename>RandomBucketingBias.cpp</filename>
            <type>text/plain</type>
            <size>2639</size>
            <attacher>ajith</attacher>
            
              <data encoding="base64">Ly8gVGVzdHMgU2FmYXJpIGFuZCBDaHJvbWl1bSdzIFBSTkcgZm9yIGV4cGVyaW1lbnQgYnVja2V0
aW5nIGJpYXMuCgojaW5jbHVkZSA8Y2Fzc2VydD4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUg
PGNzdGRsaWI+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDxjbGltaXRzPgojaW5jbHVkZSA8
Y21hdGg+CgojZGVmaW5lIE15UmFuZG9tKCkgKGFyYzRyYW5kb20oKSAlICgodW5zaWduZWQpUkFO
RF9NQVggKyAxKSkKCi8vIFdlYmtpdChTYWZhcmkpJ3MgUk5HLgpjbGFzcyBXZWFrUmFuZG9tIHsK
ICBwdWJsaWM6CiAgICBXZWFrUmFuZG9tKHVuc2lnbmVkIHNlZWQpCiAgICB7CiAgICAgIGluaXRp
YWxpemVTZWVkKHNlZWQpOwogICAgfQoKICAgIGRvdWJsZSBnZXQoKQogICAgewogICAgICByZXR1
cm4gYWR2YW5jZSgpIC8gKFVJTlRfTUFYICsgMS4wKTsKICAgIH0KCiAgICB1bnNpZ25lZCBnZXRV
aW50MzIoKQogICAgewogICAgICAgIHJldHVybiBhZHZhbmNlKCk7CiAgICB9CgogIHByaXZhdGU6
CiAgICB1bnNpZ25lZCBhZHZhbmNlKCkKICAgIHsKICAgICAgbV9oaWdoID0gKG1faGlnaCA8PCAx
NikgKyAobV9oaWdoID4+I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