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

在看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讲师

모든 응답(3)
阿神


이 프로그램은 의사 난수를 생성합니다. 임의의 숫자 시드는 47로 고정되어 있습니다. 물론, 달라지기를 원할 경우에도 마찬가지입니다. , 시드를 타임스탬프 등으로 변경할 수 있습니다.

迷茫

new Random(47)으로 바꾸시면 됩니다. new Random()

컴퓨터에서 생성된 난수는 모두 의사 난수입니다. 주어진 초기화 시드가 동일한 한 생성된 난수 시퀀스는 동일합니다.

Ty80

동일한 시드 번호와 동일한 수의 난수를 생성하는 무작위 개체는 완전히 동일합니다.
Random rand = new Random()으로 변경하면 됩니다.

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!