# From an email address to a quasi-random number [closed]

### My Aim:

I'd like to a have a function that takes an email address and outputs a quasi-random number of 1, 2, 3, or 4.

### A little detail:

By quasi-random number I mean that given a typical population of email addresses, the probabilities of getting a value of 1, 2, 3, or 4 are roughly equal, and that obvious systematic properties of the email address such as the domain name do not affect the probability of getting a value of 1, 2, 3, or 4.

### A little background:

I have an online experiment written in inquisit where participants log in on two occasions. I want to randomly assign participants to one of four groups. While this is easy to do for one session (I can just use a random number generator), I need some way of remembering the allocation across sessions. Thus, I thought that I could extract a quasi-random group allocation from the participant email. I'm also limited in the set of functions that I have at my disposal (see here for full list). The string functions are: tolower toupper capitalize concat search replaceall contains startswith endswith substring trim trimright trimleft length format evaluate

### Initial Thoughts:

I thought about trying to extract a set of features of the email address that returned a value of 1, 2, 3, or 4 with roughly equal probabilities. Then, I could sum these properties and get the mod 4 plus 1 of that. Thus, assuming something like the central limit theorem, I might get close.

Possible features that came to my mind:

• length of string
• position of first "a", "b", etc.
• A very interesting problem. Do you have a sample of "typical population of email addresses" at hand ? Additionally it is not guaranteed, that the email-addresses of the visitors do have the same another/different structure, but since you are only looking for an approximation.... Second question: Are you able to set the seed of the RNG ? Feb 16, 2011 at 9:26
• Sounds like you want a 'hash function': en.wikipedia.org/wiki/Hash_function This is in the realms of computer science rather than statistics though, so I'm not sure it belongs on CrossValidated. Feb 16, 2011 at 10:20
• hmpf ;) ... I intended to write the same. @Jeromy: Especially this part of the site (en.wikipedia.org/wiki/…) could be interesting for you. Feb 16, 2011 at 10:51
• @onestop Thanks for the tip about hashtags. With regards to whether the question is on topic for the site, I think random allocation of participants to groups is inherently related to study design, which in turn is related to inferences from data. Feb 16, 2011 at 14:20
• @Jeremy A hash function is not the same thing at all as a hashtag! I see your point about study design though. I admit to not reading the whole of your question properly. Feb 16, 2011 at 14:27

Look up hash functions, for example at http://en.wikipedia.org/wiki/Hash_function

Why not just have a look-up table of numbers for each possible character in an email. Then concatenate the numbers to form a seed. For example,

A 1
B 2
C 3
....
@ 27
....


So abc@ccc, would be converted to 12327333. This would give you a unique seed for each person. You would then use this to generate the 1, 2, 3, 4.

From your question, it looks like you don't mind a "quick and dirty solution". One problem with my solution is that email addresses aren't random - for example you will probably get very few email addresses that contain the letter "z", but all email addresses contain "@".

• A minor note about the above method is that there are a bunch of valid characters in email addresses - punctuation in particular - that you would want to consider if you were doing this. Feb 16, 2011 at 14:51
• @dsol: I agree. You could easily be caught out with a "+" in an email address. For a quick and dirty solution, I would probably just skip any punctuation characters that I hadn't specified in my look-up table. Feb 16, 2011 at 14:59

As an addition to other excellent answers, I just will give a simple example in R language to show a very simple hash function, which should be good enough for this purpose. To get some email addresses as test data, I get a character vector with the emails of the maintainers of the (too many!) R packages installed on my computer:

library(stringr) # on CRAN
last <- function(x) { return( x[length(x)] ) }

INST  <-  installed.packages(priority="NA", fields=c("Maintainer"))
Maintainer <- INST[, "Maintainer"]
Mlist <- str_split(Maintainer, "[[:blank:]]")


Then I define a simple function which gets some number from each character in the email address, adds them, computes the remainder modulo 4 and adds 1, so it returns always one of the results 1,2,3 or 4:

apply_to_each_char  <-  function(w, FUN) {
ww <-  str_split(w, "")[]
res <- sapply(ww, FUN)
} # END apply_to_each_char
charsum <- function(word) { # length-one char vector
sum0 <- sum( apply_to_each_char(word, function(w) as.integer(charToRaw(w)) ))
return( 1 + sum0 %% 4)
} # end charsum


Then applying it:

hashes <- sapply(Maddr, charsum)
table(hashes)
hashes
1   2   3   4
542 511 562 552


and we can observe that the resulting distribution is close to uniform.

You could try converting each character to an ascii number, multiplying them all together to force overflow, and then performing a modulus operation on the least significant digits. If this is not pseudo-random enough, you can perform a bit-shift the numbers a bit...

-Ralph Winters

• Multiplying is not the best idea, I think. Especially if your initial overflow is the regular one - modulo some power of 2. You will get a lot of factors that are even, so most of your lower bits will be 0. Adding the numbers together instead would already be a lot better. If you need even better randomness, use some sort of hash function and use any bits of the result. If you want it to be difficult to guess anything about the outcome for people other than you, use a salted strong cryptographic hash function. Feb 16, 2011 at 17:17
• Agreed. Just wanted to suggest idea to illustrate bit shifting in order to generate (roughly) pseudo-random numbers. Feb 17, 2011 at 18:48