用随机数编写程序

时间:2013-10-18 01:41:04

标签: java random

我必须为我的CS类简介编写一个程序作为项目。该计划必须

  1. 向用户询问最小值(可以为负数)
  2. 询问用户最大值(也可能是负值,但必须大于min)
  3. 产生20个随机数,介于用户和
  4. 给出的最小/最大值之间
  5. 产生20个随机数的平均值。
  6. 我以为我已经完成了程序,但是当我执行程序时,数字从不随机,所有二十个数字都与我输入emacs的最大值相同。当然,在这种情况下,平均值也与最大值相同。到目前为止,这是我的计划......

    import java.util.Scanner;
    import java.util.Random;
    
    public class CS_ProjectOne
    {
        public static void main(String[] args)
        {
        int minimum_integer;
        int maximum_integer;
        int rn1;
        int rn2;
        int rn3;
        int rn4;
        int rn5;
        int rn6;
        int rn7;
        int rn8;
        int rn9;
        int rn10;
        int rn11;
        int rn12;
        int rn13;
        int rn14;
        int rn15;
        int rn16;
        int rn17;
        int rn18;
        int rn19;
        int rn20;
        double total;
        double average;
        Scanner keeper;
        Scanner keeper2;
        Random gen;
        gen = new Random();
    
        keeper = new Scanner(System.in);
        System.out.print("Please enter an integer for the MINIMUM value: ");
        minimum_integer = keeper.nextInt();
    
        keeper2 = new Scanner(System.in);
        System.out.print("Please enter an integer for the MAXIMUM value: ");
        maximum_integer = keeper2.nextInt();
    
        rn1 = gen.nextInt();
        rn1 = rn1 % minimum_integer + maximum_integer;
        rn2 = gen.nextInt();
        rn2 = rn2 % minimum_integer + maximum_integer;
        rn3 = gen.nextInt();
        rn3 = rn3 % minimum_integer + maximum_integer;
        rn4 = gen.nextInt();
        rn4 = rn4 % minimum_integer + maximum_integer;
        rn5 = gen.nextInt();
        rn5 = rn5 % minimum_integer + maximum_integer;
        rn6 = gen.nextInt();
        rn6 = rn6 % minimum_integer + maximum_integer;
        rn7 = gen.nextInt();
        rn7 = rn7 % minimum_integer + maximum_integer;
        rn8 = gen.nextInt();
        rn8 = rn8 % minimum_integer + maximum_integer;
        rn9 = gen.nextInt();
        rn9 = rn9 % minimum_integer + maximum_integer;
        rn10 = gen.nextInt();
        rn10 = rn10 % minimum_integer + maximum_integer;
        rn11 = gen.nextInt();
        rn11 = rn11 % minimum_integer + maximum_integer;
        rn12 = gen.nextInt();
        rn12 = rn12 % minimum_integer + maximum_integer;
        rn13 = gen.nextInt();
        rn13 = rn13 % minimum_integer + maximum_integer;
        rn14 = gen.nextInt();
        rn14 = rn14 % minimum_integer + maximum_integer;
        rn15 = gen.nextInt();
        rn15 = rn15 % minimum_integer + maximum_integer;
        rn16 = gen.nextInt();
        rn16 = rn16 % minimum_integer + maximum_integer;
        rn17 = gen.nextInt();
        rn17 = rn17 % minimum_integer + maximum_integer;
        rn18 = gen.nextInt();
        rn18 = rn18 % minimum_integer + maximum_integer;
        rn19 = gen.nextInt();
        rn19 = rn19 % minimum_integer + maximum_integer;
        rn20 = gen.nextInt();
        rn20 = rn20 % minimum_integer + maximum_integer;
    
        System.out.println("Your random numbers are: " + rn1 + ", " + rn2 + ", " + rn3 + ", " + rn4 + ", " + rn5 + ", " + rn6 + ", " + rn7 + ", " + rn8 + ", " + rn9 + ", " + rn10 + ", " + rn11 + ", " + rn12 + ", " + rn13 + ", " + rn14 + ", " + rn15 + ", " + rn16 + ", " + rn17 + ", " + rn18 + ", " + rn19 + ", " + rn20 + ".");
    
        total = rn1 + rn2 + rn3 + rn4 + rn5 + rn6 + rn7 + rn8 + rn9 + rn10 + rn11 + rn12 + rn13 + rn14 + rn15 + rn16 + rn17 + rn18 + rn19 + rn20;
        average = total / 20;
    
        System.out.print("The average is " + average);
        }
    }
    

    任何人都可以提供一些帮助并引导我朝着正确的方向前进吗?我确定有些东西我做得不对(或很多东西)我只是不知道到底是什么。

    这是我试图让它工作的更短版本......

    import java.util.Random;
    import java.util.Scanner;
    
    public class p1
    {
        public static void main(String[] args)
        {
        Random gen;
        gen = new Random();
        Scanner keeper;
        int range, user_min, user_max, num1, num2, num3, total;
        double average;
    
        keeper = new Scanner(System.in);
        System.out.print("Please enter a maximum integer value: ");
            user_max = keeper.nextInt();
    
            keeper = new Scanner(System.in);
        System.out.print("Please enter a minimum integer value: ");
        user_min = keeper.nextInt();
    
        range = user_max - user_min + 1;
    
        num1 = gen.nextInt() % range + user_min;
        num2 = gen.nextInt() % range + user_min;
        num3 = gen.nextInt() % range + user_min;
    
        System.out.print("The random numbers computed are " + num1 + ", " + num2 + ", " + num3 + ", ");
        }
    }
    

    为什么不能处理否定?我的值仍不在指定的范围内? HELP?

4 个答案:

答案 0 :(得分:2)

一些提示/提示:

  1. 对20个数字使用数组。然后当你发现错误时,你只需要纠正一次而不是20次。

  2. 随机数生成器有种子。确保每次都提供不同的种子,否则随机数生成器将始终生成相同的随机数。

  3. MIN和MAX之间可以有多少个值?例如,3到7之间可以有多少个不同的值? 45和80? 3和1200?找到模式。

  4. 了解您的程序需要'%'运算符的原因。通过“gen.nextInt()%NUM”实现了什么?

答案 1 :(得分:1)

随机列表示例:
(在Python中代替Java,以便于演示。)

1   #!/usr/bin/env python
2   def spawn(user_min, user_max, rounds):
3     import random 
4     sum = 0
5     user_range = user_max - user_min + 1  
6     for i in range(rounds):
7       result = int((random.random() * user_range)) + user_min
8       sum = sum + result
9       print(str(i) + ': ' + str(result))
10     mean = sum / rounds
11    print('range: ' + str(user_range))
12    print('mean: ' + str(mean))

您感兴趣的行是:

  • 第5行:这决定了随机数的传播。
  • 第7行:这会获取范围[0.0, 1.0)的随机值;乘以所需的用户范围;然后转换为整数以“切掉”尾随小数点;然后将结果添加到范围的最小允许值。

例如:

range = (10) - (-5) + 1 = 16
a_result = (int)(0.14159265359 * 16) + -5 = 3 - 5 = -3
another_result = (int)(0.71828182845 * 16) + -5 = 11 - 5 = 6

答案 2 :(得分:0)

该行

rn1 = rn1 % minimum_integer + maximum_integer;

错了。例如,考虑生成1到6范围内的数字。然后计算

rn1 = rn1 % 1 + 6;

现在,rn1 % 1总是等于0,所以你总是得到6.你应该使用不同的公式。

答案 3 :(得分:0)

这就是我想出的......

import java.util.*;
class RandNegToPos {
    public static void main(String[] args) 
    {

            Scanner scan = new Scanner(System.in);
            int array[]=new int[20];
            System.out.println("What is the minimum? ");
            int min = scan.nextInt();

            System.out.println("What is the maximum? ");
            int max = scan.nextInt();

            if (max < min)
                System.out.println("Sorry the maximum is less than the miniumum.");

            else{

            int rnd = (int) (min + Math.random() * ((max) - min));   
            for (int i=0; i < 20 ; i++)
            {
                rnd = (int) (min + Math.random() * ((max) - min));
                array[i] = rnd;
            }
            int sum=0;
            for (int t : array)
                sum += t;
            System.out.println("Your randoms are:");
            System.out.println(Arrays.toString(array));
            System.out.println("The average is: " + (sum/20) );
            }
   }

}