106
votes
\$\begingroup\$

As a little joke in the office someone wanted a script that randomly picks a name, and said person will make a round of drinks.

Let's call the people John, Jeff, Emma, Steve and Julie.

I thought it would be funny to make a script that seems random at a quick glance, but actually always gives the same person as the output (Up to you who you choose).

Highest voted answer wins after a week

And the winner is....

Paul R with (currently) 158 votes.

The answers here are great, and if anyone else has any other ideas what haven't been posted yet, please add them, I love reading through them.

\$\endgroup\$
  • 44
    \$\begingroup\$ xkcd.com/221 \$\endgroup\$ – AstroCB Jun 16 '14 at 19:30
  • 6
    \$\begingroup\$ @AstroCB one of my favourites. Right behind bobby tables. \$\endgroup\$ – Cruncher Jun 16 '14 at 20:08
  • 50
    \$\begingroup\$ It seems like it would be sneakier if it was random, except for never picking one person. \$\endgroup\$ – Brendan Long Jun 16 '14 at 22:52
  • 6
    \$\begingroup\$ @AstroCB this one is also fantastic: dilbert.com/strips/comic/2001-10-25 \$\endgroup\$ – gilbertohasnofb Jun 18 '14 at 10:08
  • 3
    \$\begingroup\$ I went through the first page: most answers always choose John, 2nd highest is Julie, Jeff is chosen rarely and Steve by 1. Even Ray got chosen by one but nobody chose Emma. Moral of the story: when standing in a line to decide randomly who will buy the drinks, name yourself Emma. \$\endgroup\$ – Miserable Variable Jul 15 '14 at 19:33

65 Answers 65

1 2
3
0
votes
\$\begingroup\$

PHP with microtime for extra randomisation

$names = array('John', 'Jeff', 'Emma', 'Steve', 'Julie');
$choosenPerson = false;
while($choosenPerson === false){
    // Use microtime for a very random value:
    $uid= substr(microtime(true), 0, 1);
    if( isset($names[$uid]) ){ $choosenPerson = $names[$uid]; }
}

Im going for the microtime misconception. Microtime suggests that we have a very random factor, but im taking the first character which is 1. So for the upcomming years, Jeff will loose a lot of money. After that it'll be Emma :)

\$\endgroup\$
0
votes
\$\begingroup\$

Python 2

import random
names = ["John, Jeff, Emma, Steve, Julie"]
print random.choice(names)[:3]

Surprised nobody did something like this before.

"John, Jeff, Emma, Steve, Julie" is one string. Super obvious but whatevs

\$\endgroup\$
  • \$\begingroup\$ This doesn't print John, it prints Joh \$\endgroup\$ – pppery Nov 2 '15 at 14:04
0
votes
\$\begingroup\$

Python

def iterate(names):
    for name in names:
        yield lambda:name
import random
print random.choice(list(iterate("John Jeff Emma Steve Julie".split())))()

Sorry, Julie!

The name variable int the iterate function is the same one as in the lambdas, so every lambda evaluates the the last name, Julie.

\$\endgroup\$
-1
votes
\$\begingroup\$

BASH / Coreutils

Quite a beginner at CG, but worth a shot:

RANDOM=$$;
a=$RANDOM
b=9
c=`expr $a % $b `
echo $c

$$ is the PID of the running shell, so the number should be always the same

\$\endgroup\$
  • \$\begingroup\$ What happens when you restart the shell? \$\endgroup\$ – Knerd Jun 16 '14 at 12:57
  • 3
    \$\begingroup\$ Another number is printed, but always the same for that session \$\endgroup\$ – german_guy Jun 16 '14 at 12:59
  • 1
    \$\begingroup\$ This does not work because I saved these commands in a script, and bash pick-someone-b.bash uses a different PID each time. \$\endgroup\$ – kernigh Jun 16 '14 at 19:34
  • \$\begingroup\$ Sorry... while doing the &&->$$$$ change the preview of the hidden text fooled me and showed only 2 $s... now there are 4 of then... can you check what happens in your browser with the preview of $$ while editing? If it is not a local browser problem, maybe StackExchange should get a hint about this... (Plus Megawink vom Niederrhein!) \$\endgroup\$ – user19214 Jun 17 '14 at 22:12
  • \$\begingroup\$ Maybe it went wrong because I did it on the phone. Now it seems to work. Grüße an den Niederrhein ;) \$\endgroup\$ – german_guy Jun 18 '14 at 7:00
-3
votes
\$\begingroup\$

JavaScript:

function ActuallyNotRandom() {
    this.names = ["John", "Jeff", "Emma", "Steve", "Julie"];

    this.choose = function()
    {
        return this.names[Math.random(this.names.length) * 0];
    }

    return this.choose();
}

console.log("Random person: " + ActuallyNotRandom());

Pretty straightforward, takes a list of names, makes a random number between 0 and the number of names in the list, and then multiplies it by 0 to always return 0. Will therefore actually always return the first name in the list and display it to console.

\$\endgroup\$
  • 4
    \$\begingroup\$ The *0 is very obvious. I suggest Math.floor(Math.random()) to hide it just a bit better \$\endgroup\$ – Martijn Jun 17 '14 at 12:09
  • \$\begingroup\$ Not that it matters after multiplying by 0, but Math.random(n) doesn't return return what you think it does. \$\endgroup\$ – Dennis Oct 3 '14 at 2:00
1 2
3

Not the answer you're looking for? Browse other questions tagged or ask your own question.