Random seed Math.random in Java

前端 未结 2 505
刺人心
刺人心 2020-12-11 19:12

In my code I use random numbers in different classes. How to define random seed? Can I define this seed for all the classes in the main code?

double rnd = Ma         


        
2条回答
  •  温柔的废话
    2020-12-11 19:34

    public class MathRandomWithSeed {
    
        public static void main (String args[]){
    
            int min = 5;
            int max = 100;
            int seed = 5;
            
            int random = randomNext(min, max, seed);
    
            System.out.println("Random = " + random);
        }
    
        private static int randomNext(int min, int max, int seed){
    
            int count = (max - min) / seed;
    
            int random = ((int)(count * Math.random()) * seed) + min;
    
            return random;
        }
    }
    

提交回复
热议问题