Home  >  Q&A  >  body text

Java generates 6-digit non-repeating random numbers, please help friends

I know that there is nothing that does not repeat in the cycle~, and the repetition rate can only be reduced, but I still have to give it a try~, are there any Taoist friends who can give me a try~

曾经蜡笔没有小新曾经蜡笔没有小新2670 days ago1078

reply all(5)I'll reply

  • 仅有的幸福

    仅有的幸福2017-06-28 09:25:14

    I think the meaning of randomness is that the two result values ​​before and after are unpredictable. Generally speaking, knowing A1 A2 cannot deduce the meaning of A3.
    The problem of repeated values ​​you mentioned should be measured from the perspective of probability. If the probability of obtaining each value is equal, it means "random". Repeating the results twice does not mean that it is not "random" enough.

    If you must strictly limit the value to be non-duplicate, you need a storage place, and this non-duplication must also have a time or space range.

    reply
    0
  • 某草草

    某草草2017-06-28 09:25:14

    I previously saw an algorithm for randomly generating non-repeating numbers on CSDN:

    int startArray[] = {0,1,2,3,4,5,6,7,8,9};//seed array    
    int N = 6;//随机数个数    
    int resultArray[] = new int [N];//结果存放在里面    
    for(int i = 0; i < N; i++)    
    {    
        int seed = random(0, startArray.length - i);//从剩下的随机数里生成    
        resultArray[i] = startArray[seed];//赋值给结果数组    
        startArray[seed] = startArray[startArray.length - i - 1];//把随机数产生过的位置替换为未被选中的值。    
    }  

    reply
    0
  • 阿神

    阿神2017-06-28 09:25:14

    A stupid way is to directly initialize the array, put each number in the correct position, and then randomly select it. The first result is exchanged with the last digit, and the second result is exchanged with the second to last digit. Of course, after the exchange, a random selection will be made next time The range of the number will also be reduced by one accordingly.

    As for the efficiency, the poster can implement it.

    public static void main(String[] args) {
    
        // 初始化数组
        int[] arr = new int[1000000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
    
        int randomCount = 1000;                         // 要生成多少个随机数
        Random random = new Random();                   // 随机数生成器
        long startTime = System.currentTimeMillis();    // 计时
    
        for (int i = 0; i < randomCount; i++) {
            // 随机挑选
            int pickIndex = random.nextInt(arr.length - i);
    
            // 交换
            int t = arr[pickIndex];
            arr[pickIndex] = arr[arr.length - 1 - i];
            arr[arr.length - 1 - i] = t;
        }
    
        System.out.println("take time: " + (System.currentTimeMillis() - startTime) + " ms");
    
        // 输出结果(超过一万就不输出了,直接看耗时)
        if (randomCount < 10000) {
            for (int i = 0; i < randomCount; i++) {
                System.out.printf("%06d ", arr[arr.length - i - 1]);
                if (i % 40 == 39) {
                    System.out.println();
                }
            }
        }
    }

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-28 09:25:14

    If the life cycle is long, 100,000 will be used up easily. After using up, it will be repeated no matter how you generate it.

    1. The simplest, Math.random()*10_0000, and then convert to int. There is no guarantee that it will not be repeated

    2. increment, similar to the primary key increment of mysql. Starting from 1, if there are less than six digits, use 0 to make up the front

    3. Advanced point. The six-digit number can be combined using various influencing factors. Of course, this number is relatively short and there are not many factors to consider.

    Finally, give a reference link, distributed system ID generation. This may not be related to your problem, but it also has some reference.

    reply
    0
  • 怪我咯

    怪我咯2017-06-28 09:25:14

    ThreadLocalRandom.current().ints(0, 100).distinct().limit(6).forEach(System.out::println);

    0 to 100 is the range, 6 is the number. What's more important is thread safety.

    reply
    0
  • Cancelreply