【问题标题】:How do I make sure 4 random variables never equal each other?如何确保 4 个随机变量永远不相等?
【发布时间】:2017-09-17 12:48:31
【问题描述】:

我有 4 个随机变量...

int img1 = (int) Math.round((Math.random() * images.length)-1);
int img2 = (int) Math.round((Math.random() * images.length)-1);
int img3 = (int) Math.round((Math.random() * images.length)-1);
int img4 = (int) Math.round((Math.random() * images.length)-1);

我需要确保它们永远不会彼此相等,即使它们是随机的。我正在制作一个应用程序,其中有 4 个显示随机形状的图像视图(保存在可绘制文件夹中)。下面是我的应用程序的图片,以便您更好地理解我的意思...

底部的 4 个形状永远不会相同,但是我需要它们在应用程序运行时随机显示。这是我目前的代码...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    shape1 = (ImageView) findViewById(R.id.shape1);
    shape2 = (ImageView) findViewById(R.id.shape2);
    shape3 = (ImageView) findViewById(R.id.shape3);
    shape4 = (ImageView) findViewById(R.id.shape4);
    guessShape = (ImageView) findViewById(R.id.guessShape);
    shapes[0] = shape1;
    shapes[1] = shape2;
    shapes[2] = shape3;
    shapes[3] = shape4;

    //store all the shapes in an array
    int[] images = new int[] {R.drawable.img_0, R.drawable.img_1, R.drawable.img_2, R.drawable.img_3, R.drawable.img_4,
    R.drawable.img_5, R.drawable.img_6, R.drawable.img_7, R.drawable.img_8, R.drawable.img_9, R.drawable.img_10,
            R.drawable.img_11, R.drawable.img_12, R.drawable.img_13, R.drawable.img_14, R.drawable.img_15, R.drawable.img_16,
            R.drawable.img_17};

    int[] outlines = new int[] {R.drawable.outline_0, R.drawable.outline_1, R.drawable.outline_2,
            R.drawable.outline_3, R.drawable.outline_4, R.drawable.outline_5, R.drawable.outline_6,
            R.drawable.outline_7, R.drawable.outline_8, R.drawable.outline_9, R.drawable.outline_10,
            R.drawable.outline_11, R.drawable.outline_12, R.drawable.outline_13, R.drawable.outline_14,
            R.drawable.outline_15, R.drawable.outline_16,R.drawable.outline_17};

    //generate random number between 0 and image.length

    int img1 = (int) Math.round((Math.random() * images.length)-1);
    int img2 = (int) Math.round((Math.random() * images.length)-1);
    int img3 = (int) Math.round((Math.random() * images.length)-1);
    int img4 = (int) Math.round((Math.random() * images.length)-1);
    System.out.println(img1);
    System.out.println(img2);
    System.out.println(img3);
    System.out.println(img4);


    int whichImg = (int) Math.round((Math.random() * 4));

    System.out.println(whichImg);

    if(whichImg == 1){
        whichImg = img1;
    } else if(whichImg == 2){
        whichImg = img2;
    } else if(whichImg == 3){
        whichImg = img3;
    } else {
        whichImg = img4;
    }

    int outlineID = outlines[whichImg];

    //conditions so that the 4 boxes don't have the same image
    if(img1 == img2 && img1 !=0 || img1 == img3 && img1 != 0 || img1 == img4 && img1 != 0){ //if img1 and another img is the same

        img1 = img1 - 1;
    }
    else if(img2 == img3 && img2 != 0 || img2 == img4 && img2 != 0){ //if img2 and another img is the same
        img2 = img2 - 1;
    }

    else if(img3 == img4 && img3 !=0){ //if img3 and another image is that same
        img3 = img3 - 1;
    }
    else if(img1==0 && img2==0 && img3 == 0 && img4 == 0){ //if all the images were 0
        img1 = img1 + 10;
        img2 = img2 + 5;
        img3 = img3 + 7;
        img4 = img4 + 14;
    }
    else if(img1==img2 && img2 == img3 && img1 !=0 && img1 != 17){ //if img1 and 2 others were the same
        img1 = img1 - 1;
        img3 = img3 + 1;
    }
    else if(img2==img3 && img3 == img4 && img2 != 0 && img2 != 17){ //if img2 and 2 others were the same
        img2 = img2 - 1;
        img4 = img4 + 1;
    }
    else if(img3 == img4 && img4 == img2 && img3 != 0 && img3 != 17){ //if img3 and 2 others were the same
        img3 = img3 - 1;
        img2 = img2 + 1;
    }
    else if(img1 == 17 && img2 == 17 && img3 == 17 && img4 == 17){
        img1 = img1 - 1;
        img2 = img2 - 2;
        img3 = img3 -3;
    }
    else {
        System.out.println("Finished comparing 4 variables");//this doesnt work..fix!
    }

    //set the image
    guessShape.setBackgroundResource(outlineID);
    shape1.setBackgroundResource(images[img1]);
    shape2.setBackgroundResource(images[img2]);
    shape3.setBackgroundResource(images[img3]);
    shape4.setBackgroundResource(images[img4]);

必须有一种更简单的方法来确保它们永远不会相互相等,而不是使用这么多的 if 语句。请帮帮我!

【问题讨论】:

  • 解释一下你现在用来处理问题的逻辑,或者你尝试了什么,然后我们可以帮助你改进它。
  • 我使用了 if 语句 @coinbird,但看起来要写很多,我是编码新手(精确到 3 个月),我想构建我的第一个应用程序。但是我知道编码是合乎逻辑的,必须有一种更简单的方法来确保 4 个随机数变量永远不会相同。

标签: java android function math random


【解决方案1】:

这里有两种可能性:

首先:创建一个包含所有随机数的列表,然后遍历它。

final List<Integer> collect = IntStream.range(0, args.length).boxed().collect(Collectors.toList());
Collections.shuffle(collect);

或者,只需得到 4:

Set<Integer> randoms = new HashSet<>();
Random r = new Random();
while(randoms.size() < 4){ randoms.add(r.nextInt(args.length)); }

【讨论】:

  • 我喜欢第二个选项。现在我实现了它,我在游戏中使用了什么?我要放 shape1.setBackgroundResource(randoms.get(0)); 吗?
  • 这似乎合乎逻辑。
  • 我无法从 randoms 中获取元素?
  • 我的意思是,我说我实际上无法从您建议的称为“随机”的 HashSet 中获取元素。你对我说“这似乎合乎逻辑”,问你我接下来要做什么,但事实上,没有办法从 HashSet 中“获取”一个元素。你直率的回答表明你没有更多的时间来解决我的问题。还是干杯吧朋友
  • 看来您不知道区别,也不知道如何使用 Sets 与 Lists - 是的,我没有时间回答。你应该自己研究一下,因为有很多很好的文档。您可以获取 Set 的元素并将它们移动到 List 中,或者使用迭代器遍历 Set 的元素。阅读这个问题,尤其是第三个答案:stackoverflow.com/questions/8185090/…
【解决方案2】:

如果图片的总数很大,那么每次选择一张新图片时,只需检查它是否已经在您的集合中。万一出现这种情况,请拒绝它并重新选择。

如果图像的总数不是很大,那么重复发生的可能性也不是很大。在这种情况下,从索引 0 到 (n-1) 中选择第一个。然后将您选择的那个交换到位置 0。接下来,从索引 1 到 (n-1) 中选择第二个。然后把那个换到位置 1。继续直到你完成了你的设置,在你的情况下,这将是位置 0 到 3。(这个算法被称为Fisher-Yates shuffle。)

【讨论】:

    【解决方案3】:

    如果我正确理解您的问题,即您需要ANY四个彼此不相等的随机数,您可以试试这个:

    int max = 4; // the maximum number of the random numbers
    Set<Integer> uniqueCollection = new HashSet<Integer>();
    while(uniqueCollection.size()<max){
        uniqueCollection.add((int)(Math.random()*max*10)+1); // the 1 here for 0 values if any
    }
    for(Integer r : uniqueCollection){ // to test it
        System.out.println(r);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-06
      • 2020-12-14
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 2010-11-18
      相关资源
      最近更新 更多