# Correlated random numbers

By: Alexandre Gilotte / 05 Feb 2016

Hunting a bug in one of our metrics, we recently discovered a very unexpected correlation in our data, between two actions that we make using pseudo random numbers. Isolating the code responsible for this, we discovered that we had indeed a very tricky bug in our use of pseudo random numbers.

After removing all the irrelevant code, it finally looked like this:

``````        const int SeedShift = 655575;
static void GetRandomNumbers(int seed, out double x , out double y)
{
x = new Random(seed).NextDouble();
y = new Random(seed + SeedShift).NextDouble();
}
``````

Variables x and y were used to take two distinct random actions., and the seed here was built from a hash of the id of the impression. Note that seeding the random like this is a good practice, mandatory to reproduce what happens in a complex prod environment.

We were expecting here that x and y behaved as two (almost) independent random numbers. In other words, we expected the graph of x versus y to look like this: But let’s see what actually happened: ``````
public static void Main(string[] args)
{
Random seedGenerator = new Random();
for (int i = 0; i < 1000; i++)
{
int seed = seedGenerator.Next();
double x,y;
GetRandomNumbers(seed, out x, out y);
Console.WriteLine("" + x + ";" + y);
}
}
``````

Plotting the resulting (x,y) pairs, we got this: As you can see, x and y were definitely NOT independent. What it meant for us at criteo, was that some metrics we computed and which relied on the independence of the actions were just wrong. Fortunately, these were not critical metrics, and we did not observe a significant difference in business metrics (clicks, sales,…) after fixing this bug.

How could a largely used object such as C# Random exhibit such a bad behavior ?

The answer is simple: Random object was not designed to be used this way. The rule of thumb to remember here is: your random numbers are just as random as your seeds. Here the seeds were correlated, and so were the random numbers.

For more details, you can have a look at : http://stackoverflow.com/questions/12282628/why-are-initial-random-numbers-similar-when-using-similar-seeds

How to fix this ?

One obvious solution is to use only one random generator:

``````
const int SeedShift = 655575;
static void GetRandomNumbers(int seed, out double x, out double y)
{
var random = new Random(seed);
x = random.NextDouble();
y = random.NextDouble();
}
``````

However, we did not use that. Remember indeed that this is a very simplified version of the code: actually both random objects were called in different threads. Using the same random in different thread would lose the reproducibility of our system. Note also that C# random object is not thread safe. Instead we finally used something like this:

``````
static void GetRandomNumbers(int seed, out double x, out double y)
{
int seedX = Hash( seed ,"uniqueSaltForX");
int seedY = Hash( seed ,"uniqueSaltForY");
x = new Random(seedX).NextDouble();
y = new Random(seedY).NextDouble();
}
``````

Here the ‘Hash’ function is a hash strong enough to guarantee that seedX and seedY can be considered as independent random numbers. Another possibility would be to use seedX and seedY directly as your random numbers. (But in our case, one of the actions actually required several random numbers.)

And by the way, I mentioned that C# Random object is not thread safe. If you think it is not a big deal, you may have a look at the following program:

``````
static void CallRandom(Random random, int threadId)
{
for (int i = 0; i < 100000; i++) // A lot of calls to be sure the race conditions in random happened …
{
random.NextDouble();
}
for (int i = 0 ; i < 10 ; i++)
{
}
}

static void Main(string[] args)
{
Random random = new Random();
{"ft_lang_label":"__label__en","ft_lang_prob":0.9168909,"math_prob":0.9494443,"size":4006,"snap":"2023-40-2023-50","text_gpt3_token_len":925,"char_repetition_ratio":0.13468266,"word_repetition_ratio":0.057142857,"special_character_ratio":0.24887669,"punctuation_ratio":0.15006468,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9854051,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-09T15:18:06Z\",\"WARC-Record-ID\":\"<urn:uuid:1de2eb2c-eb4a-4a32-a3ab-d6118372ccc6>\",\"Content-Length\":\"63662\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7f76da45-af45-48d3-8c55-251e3b73f80b>\",\"WARC-Concurrent-To\":\"<urn:uuid:45c6a56e-9903-4fe8-8bcd-15cf4fb8932e>\",\"WARC-IP-Address\":\"87.98.154.146\",\"WARC-Target-URI\":\"https://labs.criteo.com/2016/02/correlated-random-numbers-3/\",\"WARC-Payload-Digest\":\"sha1:D5NSDV6NLJT3EKYP52AHUDDRLP2UZWTY\",\"WARC-Block-Digest\":\"sha1:RVFZQ4V3HBL3DTKXFGAISQ4QAJQDYUJV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100912.91_warc_CC-MAIN-20231209134916-20231209164916-00690.warc.gz\"}"}