An urgent need was recently encountered as follows: Companies need to online for a new activity, ten yuan spell, three people together, in order to improve the user spell success, stimulate the purchase, each user to open the link, according to spell group, has two virtual users at the same time, the virtual avatars take the random 500 head, and request the same user in have spell group 2 avatars unchanged.

Soon, as a researcher, I understood the demand and was ready to start construction. As this need is urgent, it will be handled in the simplest way possible. So we direct to 500 head, direct entry to the database, then found that eventually become the algorithm to remove a picture, that is random, at the same time to ensure that the results of the same users get random forever, as an experienced r&d staff and can be thought of using userId to exploit, at this time we use to write a piece of code

public class Demo { public static void main(String[] args) { User user = new User(); user.setId(1234L); user.setName("ganhuojun"); user.setMoblie("13912345678"); for (int i = 0; i < 10; i++) { System.out.println(Math.abs(user.getId().hashCode() % 500)); System.out.println(Math.abs(user.getName().hashCode() % 500)); System.out.println(Math.abs(user.getMoblie().hashCode() % 500)); }}}Copy the code

The User for the User

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    private Long id;

    private String name;

    private Integer age;

    private String address;

    private String moblie;

}
Copy the code

The result for running code is always a determined value, as shown below

234
157
165
Copy the code

In this way, we ensure that the avatar of the group stays the same whenever the user accesses it, and does not use the database.

Soon, someone asked users what if they changed their user name and phone number?

The userId should be unique to the user, and the question then becomes whether the userId can be used alone to get several fixed random values. Therefore, we turn to the Random class, as shown in the following figure

The Random() constructor, which has no arguments, finally accesses a constructor with arguments,

public Random(long seed) {
    if (getClass() == Random.class)
        this.seed = new AtomicLong(initialScramble(seed));
    else {
        // subclass might have overriden setSeed
        this.seed = new AtomicLong();
        setSeed(seed);
    }
}

synchronized public void setSeed(long seed) {
   this.seed.set(initialScramble(seed));
   haveNextNextGaussian = false;
}
Copy the code

Seed is found to be the seed of random number through source code. Once the seed is the same, the result is the same. The algorithm is as follows

So it immediately occurred to me that if we were to seed the user’s userId, we would get the same random value for every request, so we changed the code to

public class Demo { public static void main(String[] args) { User user = new User(); user.setId(1234L); user.setName("ganhuojun"); user.setMoblie("13912345678"); Random random = new Random(); for (int i = 0; i < 10 ; i++) { random.setSeed(user.getId()); System.out.println(random.nextInt(500)); // 128 System.out.println(random.nextInt(500)); // 133 System.out.println(random.nextInt(500)); // 133}}}Copy the code

Note the removal of redo operations.

conclusion

The seed property of random number is mainly used to ensure that the result of each request is unchanged, so as to avoid data storage operation and achieve twice the result with half the effort.

Welcome to pay attention to my public number, a only share dry goods public number ~~~