面对对象 - java这段代码为什么不随机的
PHP中文网
PHP中文网 2017-04-17 17:52:36
0
3
345

在看Thinking in Java,有一段实在没看懂

package com.company.allAreTheObjective.Symbol; import java.util.*; /** * Created by Francis on 12/05/2016. */ public class VowelsAndConsonants { public static void main(String args[]){ Random rand = new Random(47); for (int i = 0; i < 100; i++){ int c = rand.nextInt(26) + 'a'; System.out.print((char)c+","+ c +":"); switch(c){ case 'u' : System.out.println("vowel");break; case 'w' : System.out.println("Sometimes a vowel");break; default : System.out.println("constant"); } } } }

这一段为什么输出恒为
y,121:constant
n,110:constant
z,122:constant
b,98:constant
r,114:constant
n,110:constant
y,121:constant

PHP中文网
PHP中文网

认证高级PHP讲师

reply all (3)
阿神


The program generates pseudo-random numbers. Your random number seed is fixed at 47. Of course, it will be the same every time. If you want it to be different, you can change the seed to a timestamp or something like that

    迷茫

    Just put itnew Random(47)改成new Random().

    The random numbers generated by the computer are all pseudo-random numbers. As long as the initialization seed given is the same, the generated random number sequence will be the same.

      Ty80

      Random objects with the same seed number, the random numbers generated the same times are exactly the same.
      Just change it to Random rand = new Random().

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!