android: generate random number without repetition

后端 未结 4 1991
隐瞒了意图╮
隐瞒了意图╮ 2021-01-07 00:03

can anybody help me in making a method to generate random number without repetition in Android? The maximum number is: prjcts.size(); it\'s my JSON Array. And t

相关标签:
4条回答
  • 2021-01-07 00:16

    For what its worth, you could try using the Mersenne Twister algorithm, which has a Java implementation of it here. The Mersenne Twister is a random number generator which has a period of 2^19937 − 1, so you're pretty much guaranteed to not get the same random number.

    0 讨论(0)
  • 2021-01-07 00:21

    I mentioned in your other question how to do this..

    List<Integer> list = new ArrayList<Integer>();
    int jsonMax = prjcts.size();
    for(int i = 1; i<=jsonMax; i++)
        list.add(i);
    
    Collections.shuffle(list);
    
    for(int i=0; i<jsonMax; i++) {
        int n = list.get(i);
        //n is a random, unique number between 1 and prjcts.size()
    }
    
    0 讨论(0)
  • 2021-01-07 00:30

    Have you tried just using Math.random()?

    Just do some casting magic and you'll be good to go:

    int index = (int)((double)prjcts.size() * Math.random());
    

    Edit:

    If you want prevent repetition, you could create a list with all the possible indices.

    int max = prjcts.size();
    List<int> indices = new ArrayList<int>(max);
    for(int c = 0; c < max; ++c)
    {
        indices.add(c);
    }
    

    Then each time you want a random index, just pick a random item from the list, removing it after from the list when you're done

    int arrIndex = (int)((double)indices.size() * Math.random());
    int randomIndex = indices.get(arrIndex);
    indices.remove(arrIndex);
    

    randomIndex is now guaranteed to be a never-before used index of of your JSON list.

    0 讨论(0)
  • 2021-01-07 00:33

    One way to get N random numbers with out repeat from 0 to N-1 would be to create an array of those N numbers and create a random number that will pick one index of that array. Then remove the index from that array, and continue on with the N-1 numbers and so on.

    class NoRepeatRandom
    {
      private int[] number = null;
      private int N = -1;
      private int size = 0;
      public NoRepeatRandom(int minVal, int maxVal)
      {
        N = (maxVal - minVal) + 1;
        number = new int[N];
        int n = minVal;
        for(int i = 0; i < N; i++)
          number[i] = n++;
        size = N;
      }
    
      public void Reset() { size = N; }
    
      // Returns -1 if none left
      public int GetRandom()
      {
        if(size <= 0) return -1;
        int index = size * Math.random();
        int randNum = number[index];
    
        // Swap current value with current last, so we don't actually
        // have to remove anything, and our list still contains everything
        // if we want to reset
        number[index] = number[size-1];
        number[--size] = randNum;
    
        return randNum;
      }
    }
    
    void Test()
    {
      NoRepeatRandom nrr = new NoRepeatRandom(0, 10);
      for(int i = 0; i < 12; i++)
        System.out.println("Random number: " + nrr.GetRandom());
    }
    
    0 讨论(0)
提交回复
热议问题