0

I would like to assign each visitor a random number in order to enable stuff like A/B-testing etc. I also would like to generate the number on the client - not the server. And since it should be fast I would like to use Math.random()

My question now is the following: Do I get a random distribution of numbers or will different implementations skew the numbers?

Cheers Valentin

Valentin
  • 373
  • 2
  • 13
  • 2
    I think more than "random" you need "unique". I'd use a combination of current date plus random number, or maybe sha1 or something like that. – elclanrs Jul 05 '13 at 07:55
  • If you're doing it client-side with no communication to the server, you're going to have some chance of collisions. To reduce this chance, you've got to use some big numbers. – Ben Jul 05 '13 at 07:58
  • @elclanrs Getting an unique number would be a +1 for me. More important is an even distribution. Lets say 50% should see one variation and the second half should see another. – Valentin Jul 05 '13 at 09:11
  • See also http://stackoverflow.com/questions/2344312/how-is-randomness-achieved-with-math-random-in-javascript and http://stackoverflow.com/questions/1062902/how-random-is-javascripts-math-random – Spudley Jul 05 '13 at 09:55

2 Answers2

0

In general, implementations of 'Math.random()' on client side should be good enough for the random distribution as well as if it was your server that generate them. But you can not be sure.

Galigator
  • 8,957
  • 2
  • 25
  • 39
  • Any reason why you believe that to be true? I would like to believe that too! – Valentin Jul 05 '13 at 09:27
  • 1
    Looking at the source of Chromium-browser you can see the implementations of Math.random. It refere also to the random of Safari. The tests are done in a file tagged with Mozilla. So implementation should not be so different. By the way the norm should be this : http://es5.github.io/x15.8.html#x15.8.2.14 so the problem really relies on seeds of random. Being distributed improve varity and seed, So the random distribution should as good as on single server. – Galigator Jul 06 '13 at 09:32
0

You can use the follow javascript to test the effective random distribution of javascript's Math.random:

var totalAbove  =0;
var totalBelow = 0
for(var i=0;i<10000;i++){
    var randomNumber = Math.ceil(Math.random()*1000)
    if(randomNumber > 500){
        totalAbove++
    }else{
        totalBelow++
    }
}

var belowSplit = (totalBelow / 10000) * 100
var aboveSplit = (totalAbove / 10000 ) * 100
alert("Above 500: " + totalAbove + "\n" + "Below 500: " + totalBelow + "\n split: " + belowSplit + "/" + aboveSplit);

or use this jsfiddle: http://jsfiddle.net/VN3Bd/

You can try it across different browsers to see if it behaves differently, but I think you'll see that the distribution is quite even.

I think you can rely on Math.random for this job.

Zappatta
  • 422
  • 7
  • 15
  • So the idea is: If all browser generate an even distribution the sum of all browser generated numbers is also evenly distributed? Is this a valid assumption? – Valentin Jul 05 '13 at 09:15
  • 2
    Yes. I've actually been using something similar and it works pretty well. As long as you are comfortable to have your distribution within a statistical range and not EXACTLY 50/50 (or whatever percentage your are looking for) – Zappatta Jul 05 '13 at 09:40