java 1 100随机数_java – 从1-50的生成器生成1-100的随机数

在最近的一次采访中,我被问到以下问题:

Print random numbers from 1-100 using the given getrnd50() method

which generates the random numbers from 1-50. Each random number

should be printed only once and in random order. Use of no other random number generator

is allowed and i was not allowed to change the definition of

getrnd50().

我想出了以下代码,它给出了正确的输出.

import java.util.Random;

public class Test {

public static void main(String[] args) {

int[] rs = new int[100];

int count = 0;

int k;

while (count != 100) {

// I decided to simply multiply the result of `getrnd50()` by 2.

// But since that would generate only even numbers,

k = getrnd50() * 2;

// I decided to randomly subtract 1 from the numbers.

// Which i accomlished as follows.

if (getrnd50() <= 25) // 25 is to half the possibilities.

k--;

// Every number is to be stored in its own unique location

// in the array `rs`, the location is `(number-1)`.

// Every time a number is generated it is checked whether it has

// already been generated. If not, it is saved in its position, printed and

// `count` is incremented.

if (rs[k-1] == 0) {

rs[k-1] = k;

count++;

System.out.print(k + " ");

}

}

}

// This is the method and i am not supposed to touch it.

static int getrnd50() {

Random rand = new Random();

return (1 + rand.nextInt(50));

}

}

虽然它在那一轮被接受,但在下一轮中,面试官告诉我getrnd50()是一种昂贵的方法,即使在最好的情况下,我必须为每个生成的数字调用它两次.即1-100的200次.在最坏的情况下,它将是无限的,平均情况下数万.他要求我优化代码,以便显着改善平均情况.

当我表示无法做到时,他给了我一个暗示,他说:

To consider the number of numbers generated while generating a new

number. For ex. if count becomes 99 i don’t have to call getrnd50() I

can simply find the remaining number and print it.

虽然我理解他的漂移我不知道它会如何帮助我,所以显然我被拒绝了.现在我很想知道答案.帮我! Thanx提前!

注意:如果有人懒得写一个冗长的代码只是指出数字生成部分,其余的很容易.此外,我们不一定遵循提示.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成不重复的随机数可以采用以下两种方法: 1. 使用java.util.Random类生成随机数,并将生成随机数存储到一个Set或者List容器中,每次生成新的随机数时都先检查容器中是否已经存在该随机数,如果已存在则重新生成,直到生成一个不在容器中的随机数。 示例代码: ```java import java.util.HashSet; import java.util.Random; import java.util.Set; public class RandomUtil { private static Set<Integer> set = new HashSet<>(); private static Random random = new Random(); private static final int MAX_NUM = 100; public static int generateRandom() { int randomNum; do { randomNum = random.nextInt(MAX_NUM); } while (set.contains(randomNum)); set.add(randomNum); return randomNum; } } ``` 2. 使用java.security.SecureRandom类生成随机数,并将生成随机数存储到一个数组中,每次生成新的随机数时,从数组中随机取出一个数,并将该位置上的数与数组末尾的数交换位置,然后缩小数组范围,以避免重复。 示例代码: ```java import java.security.SecureRandom; public class RandomUtil { private static final int MAX_NUM = 100; private static int[] arr = new int[MAX_NUM]; private static SecureRandom secureRandom = new SecureRandom(); private static int size = MAX_NUM; static { for (int i = 0; i < MAX_NUM; i++) { arr[i] = i; } } public static int generateRandom() { if (size == 0) { size = MAX_NUM; } int index = secureRandom.nextInt(size--); int randomNum = arr[index]; arr[index] = arr[size]; arr[size] = randomNum; return randomNum; } } ``` 以上两种方法都可以生成不重复的随机数,但是第二种方法使用了安全的随机数生成器,更加安全可靠。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值