简体   繁体   English

你如何让java在两个范围内创建一个随机数(例如5-10和12-17)

[英]How do you have java create a random number within two ranges (e.g. 5-10 and 12-17)

I need to create a random number within two ranges, but I do not know how.我需要在两个范围内创建一个随机数,但我不知道如何。 Could someone help me?有人可以帮助我吗?

random numbers are generated between 0-1.随机数在 0-1 之间生成。 let's make numbers<0.5 5-10 and numbers > 0.5 12-17 by simply multiplying them.让我们通过简单地将它们相乘来使 numbers<0.5 5-10 和 numbers > 0.5 12-17 。

Try this :-尝试这个 :-

class PickRandomFromRange
{
    private final List<Integer> range = new ArrayList<>();

    final void addRange(int min, int max)
    {
        for(int i = min; i <= max; i++)
        {
            this.range.add(i);
        }
    }

    int getRandom()
    {
        return this.range.get(new Random().nextInt(this.range.size()));
    }

    public static void main(String[] args)
    {
        PickRandomFromRange rir = new PickRandomFromRange();
        rir.addRange(5,10);
        rir.addRange(12,17);
        System.out.println(rir.getRandom());
    }
}

The above code will basically just makes a list of all the numbers which are in the ranges you give it, then picks a random number from that list, which is therefore a random number from any of the ranges.上面的代码基本上只是列出你给它的范围内的所有数字,然后从该列表中选择一个随机数,因此它是来自任何范围的随机数。

I figured out a way, although, I don't know if it is an efficient way of solving my problem.我想出了一个方法,虽然,我不知道这是否是解决我问题的有效方法。

            if(userInput == 4){
            charNumber = 48+random.nextInt(76);
            if(charNumber>47 && charNumber<58){
                outputFile.print((char)charNumber);
                counter++;
            }
            if(charNumber>64 && charNumber<91){
                outputFile.print((char)charNumber);
                counter++;
            }
            if(charNumber>90 && charNumber<97){
                outputFile.print((char)charNumber);
                counter++
            }

            if(charNumber>96 && charNumber<123){
                outputFile.print((char)charNumber);
                counter++;
            }
        }

The ranges 5-10 and 12-17 give you a total of 6 + 6 = 12 possible selections.范围 5-10 和 12-17 为您提供总共 6 + 6 = 12 种可能的选择。

Pick a random number from 5 to 16. If the number is 11 or more then add one to it.从 5 到 16 中选择一个随机数。如果该数为 11 或更多,则向其加一。 Adding one effectively inserts the gap in your split range.添加一个有效地在您的拆分范围中插入间隙。

Random myRand = new Random();

// Number in range [5 .. 16].
int num = 5 + myRand.nextInt(17);

// Increase numbers beyond the gap.
if (num >= 11) {
  num += 1;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何处理从文件上传的数据? 例如,计算总和? java的 - how do you work with data uploaded from files? e.g., calculate the sum? java 如何为Android应用创建标题屏幕? 例如,当应用程序像Facebook一样加载时 - How do you create a title screen for an android app? E.g. When the app is loading like Facebook 如何从 MacOS 卸载 Java 版本(例如,openjdk 15)? - How do you uninstall a version of Java (e.g., openjdk 15) from MacOS? Java:生成一个范围之间的随机日期(从当前日期/时间到随机未来日期的范围(例如,从当前日期/时间开始的5-10天) - Java: Generate a random date between a range (range starting from current date/time to a random future date (e.g 5-10 days from current date/time) Java Streams - 您是否能够进行多个终端操作(例如 forEach) - Java Streams - Are you able to have multiple terminal operations (e.g. forEach) Java中数字前的方括号是什么意思? 例如 []89 或 [1, 2, 3]89 - What do square brackets before a number mean in Java? e.g. []89 or [1, 2, 3]89 如何增加ToggleGroup中的可能选择的数量(例如RadioButtons)? - How to increase the number of possible selections within a ToggleGroup (e.g. RadioButtons)? 如何安全地为要启动的后台应用程序提供敏感信息(例如密码)? - How do you securely provide a sensitive info (e.g. password) to a background application to be started? 创建包含两个项目的列表,例如列表<String, Integer> - Create List with two items e.g. List<String, Integer> 如何用Java中的值创建类似类型驱动选择的内容(例如,图片) - How to create something like a type driven selection with values in java (e.g. picture)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM