Algorithm Java Multithread Cooperation & red packet random distribution algorithm


1. Using java multithreading to print 1 to n

The requirements are as follows:

  • For a given integer n, start n threads (numbered 1 to n respectively).

  • The working logic of the thread is: the thread numbered x can only print integer X,

  • Realize the code logic, make n threads work together, print natural sequence in order: 1, 2, 3,…, n.

thinking: multiple threads spin to wait whether the task is their turn.

public class ThreadDemo {
    //Currently executing task, global visible
    public static volatile char now;

    public static void main(String[] args) {
        //Input task
        char[] input = {'1','2','3','4','5'};
        //Number of tasks per thread
        final int n = 1;
        for (int j = 0; j < input.length; j++) {
            final int t = j;
            //Start thread task
            Thread thread = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < n; i++) {
                        //Loop wait, spin
                        while (now != input[t]) {}
                        System.out.print (input [t]); // processing tasks
                        //Modify the global state of the currently executing task
                        if (t + 1 < input.length)
                            now = input[t + 1];
                            now = input[0];
        now = input[0];

2. Write a red packet random algorithm

The requirements are as follows:

  • Please write a red packet random algorithm. Demand is: given a certain amount of money, a certain number of people, to ensure that everyone can get a certain amount of money at random.

  • For example, the red envelope of 100 yuan, 10 people grab, each share some amount. The restriction condition is that the best lucky amount cannot exceed 90% of the maximum amount. Please give the Java code implementation.

Idea: take a random number from the middle of the maximum and minimum amount, generate the corresponding number, if more, then close to the average, if less, increase a little

import java.util.Random;

public class RedBagDemo {

    //Random number seed
    static Random random = new Random();

    public static void main(String[] args) {

        long[] result = generate(100, 10, 90, 1);
        for (long l : result) {


    //Zoom in and out at random
    static long xRandom(long min, long max) {
        return sqrt(nextLong(sqr(max - min)));

    //Total amount of red envelope, number of people, maximum amount of red envelope, minimum amount of red envelope

    public static long[] generate(long total, int count, long max, long min) {

        long[] result = new long[count];

        //Take the average, add and subtract from the average
        long average = total / count;

        for (int i = 0; i < result.length; i++) {
            //The red envelope is big. It's down to the average
            if (nextLong(min, max) > average) {
                long temp = min + xRandom(min, average);
                result[i] = temp;
                total -= temp;
            } else {
                //The red envelope is smaller, and it's lower than the average
                long temp = max - xRandom(average, max);
                result[i] = temp;
                total -= temp;

        //If you don't have enough money, you can add a dollar to everyone who doesn't exceed the maximum amount
        while (total > 0) {
            for (int i = 0; i < result.length; i++) {
                if (total > 0 && result[i] < max) {

        //If the total amount is less than 0, subtract 1 from the head
        while (total < 0) {
            for (int i = 0; i < result.length; i++) {
                if (total < 0 && result[i] > min) {
        return result;

    static long sqrt(long n) {
        return (long) Math.sqrt(n);

    //Zoom in
    static long sqr(long n) {
        return n * n;

    //Next random number no more than n
    static long nextLong(long n) {
        return random.nextInt((int) n);
    //The next random number between min and Max

    static long nextLong(long min, long max) {
        return random.nextInt((int) (max - min + 1)) + min;


Wu Xie, little third master, is a rookie in the field of big data and artificial intelligence.
Please pay more attention