Random类简介及代码示例

主要是用于生成伪随机数

例如: 使用户能够得到一个随机整数:
Random random = new Random();
// 范围是 int的取值范围 [0 , 2的32次方]
int i = random.nextInt();

查看类
java.util.Random 该类需要导入后才能使用
查看类的构造方法
public Random(): 创建一个新的随机数生成器
查看成员方法
public int nextInt(int n); 返回一个伪随机整数,范围在[0,n-1]
public double nextDouble(); 返回一个伪随机小数,范围在[0.0 , 1.0);



package API;

import java.util.Arrays;
import java.util.Random;

public class RandomDemo {
    public static void main(String[] args) {
        // 随机生成 10 个 10 以内的数字 ,
        int[] arr = new int[10];
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(random.nextInt(10));
            arr[i] = random.nextInt(10);
        }
        for (int value : arr) {
            System.out.print(value);
        }
        // 从大到小排序
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if(arr[i] > arr[j]){
                    int temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;
                }
            }
        }
        System.out.println();
        for (int value : arr) {
            System.out.print(value);
        }

        System.out.println();
		// 从小到大排序
        Arrays.sort(arr);

        for (int value : arr) {
            System.out.print(value);
        }
        // 练习获取 1 ~ n之间的随机数,包含n
        int n = 50;
        // public int nextInt(int n); 返回一个伪随机整数,范围在[0,n-1]
        int num = random.nextInt(n)+1;
        System.out.println(num);

    }
}

剪刀石头布游戏

package todayHw2;

import java.util.Random;
import java.util.Scanner;

public class SJB {
    public static void main(String[] args) {
        Random random = new Random();
        int Systemnum = random.nextInt(2);

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入0石头 1剪刀 2布");

        int Usernum = sc.nextInt();

        if(Systemnum == 0){
            if(Usernum ==2){
                System.out.println("win");
            }else if(Usernum == 0){
                System.out.println("no winner");
            }else{
                System.out.println("lose");
            }
        }

        if(Systemnum == 1){
            if(Usernum ==0){
                System.out.println("win");
            }else if(Usernum == 1){
                System.out.println("no winner");
            }else{
                System.out.println("lose");
            }
        }


        if(Systemnum == 2){
            if(Usernum ==1){
                System.out.println("win");
            }else if(Usernum == 2){
                System.out.println("no winner");
            }else{
                System.out.println("lose");
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值