📜 ⬆️ ⬇️

How to hold a raffle among Java programmers

Have you participated in a lottery or a draw? Did you have to arrange them yourself? Even if the answers: never and not sure, you know what it is.

And what are your associations from the words "lottery" and "draw"? I have multicolored balls with numbers and a lottery machine, from which multicolored balls fall out one by one and determine the winner.

So it took me some time ago to “determine” the winners of the free space draw for the “Java Developer” course at Otus.ru. The task sounded simple: there are N emails, you need to select among them randomly M emails of those who will study for free.
')
The difficulty of the task was that these were the emails of all those who successfully passed the input test of the course. That is, email-s programmers. I imagined how I “took out of my pocket” M emails and said: “These have won.” And ... nobody believes me. Even if the winners start to happily write to the general chat: “Thank you, we are glad!”, I still don’t believe the rest. Yes, I would not have believed it myself, if I was just told “ these have conquered”.

image

It’s not enough for programmers to say who won, it’s necessary to prove that these are really random winners, and that their email was really on the list, and that the probability of getting to winners is equal to everyone.

In such conditions, it was necessary to think of how to play the places. In this article, I want to offer you my solution to this problem. I would be happy for comments and especially comments about the credibility of my decision. Would you reasonably argue with the results if your email were on the general list?

So, how to draw prizes among programmers:

  1. Remove from the selection of the organizer of the draw. Should choose a robot.
  2. Make the robot open so that anyone can see how it chooses.
  3. Make a random selection pseudo-random. So that anyone who knows the seed of a sequence can repeat it.
  4. And at the same time, to form pseudo-randomness grain on material randomness.
  5. Share ... email? So that everyone can find themselves in the list? No, our users would not appreciate this. But somehow give a check that you are on the list, you need.

The first thing that was logical to do was to entrust the choice to the program. Write a lottery machine that would decide who won. You can see the codes of my car in my account on github .

It consists of a class for reading emails, a class that makes a selection of “balls” with indexes of winners and a class that runs everything.

Getting a list of emails is simple. They must be read from the file. At first I wanted to read emails and names from the csv file, but then I left only emails, and the csv and the library that reads it remained.

The result of reading email-s - List I pass to the lottery machine. In addition, she gets the number of winners at the entrance. And she can also give her a seed — a seed of a pseudo-random sequence. It can be set by number or by string. In the second case, the grain will be hash from the string.

If you have a seed, the lottery machine will give out one and the same “random” sequence of winners each time it starts. Random, but pseudo. What, in fact, we need.

Well, we have a list with emails, a lottery machine, and it uses the java.util.Random class, which gives us a pseudo-random sequence based on the seed.

Now, for success, we lack a simple thing: a random seed — a grain of succession. So that he was random, but so that we could remember him. I decided to use the contents of the chat as a source of such material chance. Send the last few messages to the machine so that it calculates its seed.

Anyone who came to the rally could write to the chat anytime and anywhere. Forge messages in the chat, so that podgadat with the definition of hash-ah? I do not know how to do that. In addition, I asked anyone who wanted to write something in the chat right before starting the car.

As it was, you can look at the recordings of the Open Day on youtube .

We launched a lottery machine and got winners. Then they ran it again and got the same ones.

After that, I proposed to change the text, and deleted the last message. With the words: “now let's see how this last comment influenced the result,” I started the car and among the results was an email from the one who wrote this last comment. If he had not written it, he would have a prize. Of course he noticed it. Of course we all noticed. And the winners thanked the author of the comment for the gift.

Of course, we didn’t post emails on github. But in the repo you can see the file in which “obfuscated” emails are recorded.

And how would you play prizes among programmers? Is the code and how the machine works?

Source: https://habr.com/ru/post/331478/


All Articles