随机不会重复最后一个随机的随机

发布于 2025-01-27 10:11:58 字数 317 浏览 3 评论 0原文

我正在使用Java Swing中的一个简单程序,该程序在您单击按钮时从数组中随机选择一个单词。问题是我不想在再次单击按钮后产生相同的内容。例如,您单击该按钮,然后从数组中生成“甜甜圈”一词,然后再次单击,然后再次生成甜甜圈。我想避免在之后直接生成同一事物。

这是代码:


import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;

class Main implements ActionListener{
  JFrame f = new JFrame("ℂ
              

I am working on a simple program in Java swing that randomly selects a word from an array when you click a button. The issue is that I do not want to have the same thing generated after clicking the button again. For example, You click the button and it generates the word ‘donut’ from the array, you click again, and it generates donut again. I want to avoid generating the same thing directly after.

Here is the code:


import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;

class Main implements ActionListener{
  JFrame f = new JFrame("ℂ???????????????????????????????? ????????????????????????????????????");
  Random r = new Random();
  JButton generate = new JButton("Generate");
  JButton clear = new JButton("Clear");

  JLabel characterClass = new JLabel("Style", JLabel.CENTER);
  JLabel characterClass2 = new JLabel("Race", JLabel.CENTER);
  JLabel characterClass3 = new JLabel("Weapon", JLabel.CENTER);
  
  JLabel newGenerate = new JLabel("?", JLabel.CENTER);
  JLabel newGenerate2 = new JLabel("?", JLabel.CENTER);
  JLabel newGenerate3 = new JLabel("?", JLabel.CENTER);

  Color custBlue = new Color(10, 63, 250);
  Color custOrange = new Color(251,167,88);
  Color saturatedCustOrange = new Color(254, 127, 46);
  ImageIcon wheel = new ImageIcon("images/loading_wheel.gif");
  JLabel img = new JLabel(wheel);
  ImageIcon back_image = new ImageIcon("images/back_ground.png");
  JLabel img2 = new JLabel(back_image);

  String[] style = {"<html><p align='center'>Steampunk</p></html>", "<html><p align='center'>Sci-Fi</p></html>", "<html><p align='center'>Medieval</p></html>", "<html><p align='center'>Imprisoned</p></html>", "<html><p align='center'>Superpowered</p></html>", "<html><p align='center'>Villainous</p></html>", "<html><p align='center'>Primal</p></html>", "<html><p align='center'>Musical</p></html>", "<html><p align='center'>Aquatic</p></html>", "<html><p align='center'>Undead</p></html>"};

  String[] race = {"<html><p align='center'>Sorcerer</p></html>", "<html><p align='center'>Alien</p></html>", "<html><p align='center'>Knight</p></html>", "<html><p align='center'>Orc</p></html>", "<html><p align='center'>Elf</p></html>", "<html><p align='center'>Bear</p></html>", "<html><p align='center'>Human</p></html>", "<html><p align='center'>Vampire</p></html>", "<html><p align='center'>Werewolf</p></html>", "<html><p align='center'>Dwarf</p></html>"};

  String[] weapon = {"<html><p align='center'>Bow<br />And Arrow</p></html>", "<html><p align='center'>Blade</p></html>", "<html><p align='center'>Gun</p></html>", "<html><p align='center'>Nunchucks</p></html>", "<html><p align='center'>Shield</p></html>", "<html><p align='center'>Bomb</p></html>", "<html><p align='center'>Claws</p></html>", "<html><p align='center'>Mace</p></html>", "<html><p align='center'>Axe</p></html>", "<html><p align='center'>Staff</p></html>"};
  
  public static void main(String[] args) {
    Main o = new Main();
    o.gui();
  }
  public void gui(){
    f.setSize(500, 400);
    
    this.newGenerate.setBounds(132,107, 180, 40);
    newGenerate.setFont(new Font("Arial", Font.BOLD, 20));
    this.newGenerate.setForeground(saturatedCustOrange);
    f.add(this.newGenerate);

    this.newGenerate2.setBounds(304,107, 180, 40);
    newGenerate2.setFont(new Font("Arial", Font.BOLD, 20));
    this.newGenerate2.setForeground(saturatedCustOrange);
    f.add(this.newGenerate2);

    this.newGenerate3.setBounds(218,250, 180, 40);
    newGenerate3.setFont(new Font("Arial", Font.BOLD, 20));
    this.newGenerate3.setForeground(saturatedCustOrange);
    f.add(this.newGenerate3);

    characterClass.setBounds(132,50, 180, 40);
    characterClass.setFont(new Font("Arial", Font.BOLD, 20));
    this.characterClass.setForeground(saturatedCustOrange);
    f.add(this.characterClass);

    characterClass2.setBounds(304,50, 180, 40);
    characterClass2.setFont(new Font("Arial", Font.BOLD, 20));
    this.characterClass2.setForeground(saturatedCustOrange);
    f.add(this.characterClass2);

    characterClass3.setBounds(218,192, 180, 40);
    characterClass3.setFont(new Font("Arial", Font.BOLD, 20));
    this.characterClass3.setForeground(saturatedCustOrange);
    f.add(this.characterClass3);

    this.generate.setBounds(50, 240, 120, 40);
    generate.setFont(new Font("Arial", Font.BOLD, 16));
    this.generate.setForeground(custOrange);
    this.generate.setBackground(custBlue);
    this.generate.addActionListener(this);
    f.add(this.generate);

    this.clear.setBounds(50, 290, 120, 40);
    clear.setFont(new Font("Arial", Font.BOLD, 16));
    this.clear.setForeground(custOrange);
    this.clear.setBackground(custBlue);
    this.clear.addActionListener(this);
    f.add(this.clear);

    
    img.setBounds(0, -10,500,400);
    f.add(img);

    img2.setBounds(0, -10,500,400);
    f.add(img2);
       
    f.setLayout(null);
    f.setVisible(true);
  }
  public void actionPerformed(ActionEvent e){
  if(e.getSource() == this.clear){
    img.setVisible(true);
    this.newGenerate.setText("?");
    this.newGenerate2.setText("?");
    this.newGenerate3.setText("?");
  }
  else{
    this.newGenerate.setText(this.returnStyle());
    this.newGenerate2.setText(this.returnRace());
    this.newGenerate3.setText(this.returnWeapon());
    img.setVisible(false); 
  }
}

  public String returnStyle() {
    int ranNum = r.nextInt(this.style.length);

    return this.style[ranNum];
 }

  public String returnRace() {
    int ranNum = r.nextInt(this.race.length);
    
    return this.race[ranNum];
 }

  public String returnWeapon() {
    int ranNum = r.nextInt(this.weapon.length);
    
    return this.weapon[ranNum];
 }

}  

If it is confusing, the program generates a character. It randomly mixes the style of character (like medieval or sci-fi), a race (like human or vampire), and a weapon (like a blade or axe).

I specifically need help with this portion, and I preferably need to do it with a loop, since it is the only way I could think to work a loop into this (it is required for my final project.)

public String returnStyle() {
    int ranNum = r.nextInt(this.style.length);

    return this.style[ranNum];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云裳 2025-02-03 10:11:58

对于“不同的”方法,您可以使用 集合#shuffle 随机a list> list

首先需要,您将需要...

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

接下来,您将需要定义一系列“随机” list s。这允许我们在维护对“原始”值的引用时获得下一个随机值的更简单方法,例如...

    private String[] masterStyles = {"<html><p align='center'>Steampunk</p></html>", "<html><p align='center'>Sci-Fi</p></html>", "<html><p align='center'>Medieval</p></html>", "<html><p align='center'>Imprisoned</p></html>", "<html><p align='center'>Superpowered</p></html>", "<html><p align='center'>Villainous</p></html>", "<html><p align='center'>Primal</p></html>", "<html><p align='center'>Musical</p></html>", "<html><p align='center'>Aquatic</p></html>", "<html><p align='center'>Undead</p></html>"};
    private String[] masterRaces = {"<html><p align='center'>Sorcerer</p></html>", "<html><p align='center'>Alien</p></html>", "<html><p align='center'>Knight</p></html>", "<html><p align='center'>Orc</p></html>", "<html><p align='center'>Elf</p></html>", "<html><p align='center'>Bear</p></html>", "<html><p align='center'>Human</p></html>", "<html><p align='center'>Vampire</p></html>", "<html><p align='center'>Werewolf</p></html>", "<html><p align='center'>Dwarf</p></html>"};
    private String[] masterWeapons = {"<html><p align='center'>Bow<br />And Arrow</p></html>", "<html><p align='center'>Blade</p></html>", "<html><p align='center'>Gun</p></html>", "<html><p align='center'>Nunchucks</p></html>", "<html><p align='center'>Shield</p></html>", "<html><p align='center'>Bomb</p></html>", "<html><p align='center'>Claws</p></html>", "<html><p align='center'>Mace</p></html>", "<html><p align='center'>Axe</p></html>", "<html><p align='center'>Staff</p></html>"};
    
    private List<String> randomStyles = new ArrayList<>(8);
    private List<String> randomRaces = new ArrayList<>(8);
    private List<String> randomWeapons = new ArrayList<>(8);

然后,您将修改return> returnxxx方法以返回下一个随机值,除了,如果随机list isempty,则用原始值填充,然后shuffle it ...

    public String returnStyle() {
        if (randomStyles.isEmpty()) {
            randomStyles.addAll(Arrays.asList(masterStyles));
            Collections.shuffle(randomStyles);
        }
        return randomStyles.remove(0);
    }

    public String returnRace() {
        if (randomRaces.isEmpty()) {
            randomRaces.addAll(Arrays.asList(masterRaces));
            Collections.shuffle(randomRaces);
        }
        return randomRaces.remove(0);
    }

    public String returnWeapon() {
        if (randomWeapons.isEmpty()) {
            randomWeapons.addAll(Arrays.asList(masterWeapons));
            Collections.shuffle(randomWeapons);
        }
        return randomWeapons.remove(0);
    }

这可以保证值为值。从列表中,只有一旦您使用了原始list的所有值,才会开始重复,

然后我们可以将概念封装到类中,以使其更容易。 ..

public class RandomList<Value> {
    private List<Value> masterValues;
    private List<Value> randomValues;
    
    public RandomList(Value[] masterValues) {
        this(Arrays.asList(masterValues));
    }

    public RandomList(List<Value> masterValues) {
        this.masterValues = masterValues;
        randomValues = new ArrayList<>(masterValues);
        Collections.shuffle(randomValues);
    }
    
    public Value next() {
        if (randomValues.isEmpty()) {
            randomValues.addAll(masterValues);
            Collections.shuffle(randomValues);
        }
        return randomValues.remove(0);
    }
    
    public Value nextOrNull() {
        if (randomValues.isEmpty()) {
            return null;
        }
        return randomValues.remove(0);
    }
}

哪个将您的代码更改为...

    private RandomList<String> randomStyles = new RandomList<>(new String [] {"<html><p align='center'>Steampunk</p></html>", "<html><p align='center'>Sci-Fi</p></html>", "<html><p align='center'>Medieval</p></html>", "<html><p align='center'>Imprisoned</p></html>", "<html><p align='center'>Superpowered</p></html>", "<html><p align='center'>Villainous</p></html>", "<html><p align='center'>Primal</p></html>", "<html><p align='center'>Musical</p></html>", "<html><p align='center'>Aquatic</p></html>", "<html><p align='center'>Undead</p></html>"});
    private RandomList<String> randomRaces = new RandomList<>(new String [] {"<html><p align='center'>Sorcerer</p></html>", "<html><p align='center'>Alien</p></html>", "<html><p align='center'>Knight</p></html>", "<html><p align='center'>Orc</p></html>", "<html><p align='center'>Elf</p></html>", "<html><p align='center'>Bear</p></html>", "<html><p align='center'>Human</p></html>", "<html><p align='center'>Vampire</p></html>", "<html><p align='center'>Werewolf</p></html>", "<html><p align='center'>Dwarf</p></html>"});
    private RandomList<String> randomWeapons = new RandomList<>(new String [] {"<html><p align='center'>Bow<br />And Arrow</p></html>", "<html><p align='center'>Blade</p></html>", "<html><p align='center'>Gun</p></html>", "<html><p align='center'>Nunchucks</p></html>", "<html><p align='center'>Shield</p></html>", "<html><p align='center'>Bomb</p></html>", "<html><p align='center'>Claws</p></html>", "<html><p align='center'>Mace</p></html>", "<html><p align='center'>Axe</p></html>", "<html><p align='center'>Staff</p></html>"});

和您的returnxxx方法,例如……

    public String returnStyle() {
        return randomStyles.next();
    }

    public String returnRace() {
        return randomRaces.next();
    }

    public String returnWeapon() {
        return randomWeapons.next();
    }

更简单,更干净,并删除所有重复的代码...但是,这可能是比您现在更加“高级”,但是要记住的事情

For a, "different" approach, you could make use of Collections#shuffle to randomise a List of values

First, you will need...

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Next, you will need define a series of "random" Lists. This allows us a simpler method for getting the next random value while maintaining a reference to the "original" values, for example...

    private String[] masterStyles = {"<html><p align='center'>Steampunk</p></html>", "<html><p align='center'>Sci-Fi</p></html>", "<html><p align='center'>Medieval</p></html>", "<html><p align='center'>Imprisoned</p></html>", "<html><p align='center'>Superpowered</p></html>", "<html><p align='center'>Villainous</p></html>", "<html><p align='center'>Primal</p></html>", "<html><p align='center'>Musical</p></html>", "<html><p align='center'>Aquatic</p></html>", "<html><p align='center'>Undead</p></html>"};
    private String[] masterRaces = {"<html><p align='center'>Sorcerer</p></html>", "<html><p align='center'>Alien</p></html>", "<html><p align='center'>Knight</p></html>", "<html><p align='center'>Orc</p></html>", "<html><p align='center'>Elf</p></html>", "<html><p align='center'>Bear</p></html>", "<html><p align='center'>Human</p></html>", "<html><p align='center'>Vampire</p></html>", "<html><p align='center'>Werewolf</p></html>", "<html><p align='center'>Dwarf</p></html>"};
    private String[] masterWeapons = {"<html><p align='center'>Bow<br />And Arrow</p></html>", "<html><p align='center'>Blade</p></html>", "<html><p align='center'>Gun</p></html>", "<html><p align='center'>Nunchucks</p></html>", "<html><p align='center'>Shield</p></html>", "<html><p align='center'>Bomb</p></html>", "<html><p align='center'>Claws</p></html>", "<html><p align='center'>Mace</p></html>", "<html><p align='center'>Axe</p></html>", "<html><p align='center'>Staff</p></html>"};
    
    private List<String> randomStyles = new ArrayList<>(8);
    private List<String> randomRaces = new ArrayList<>(8);
    private List<String> randomWeapons = new ArrayList<>(8);

Then, you would modify your returnXxx methods to return the next random value, except, if the random List isEmpty, you fill it with the original values and then shuffle it, for example...

    public String returnStyle() {
        if (randomStyles.isEmpty()) {
            randomStyles.addAll(Arrays.asList(masterStyles));
            Collections.shuffle(randomStyles);
        }
        return randomStyles.remove(0);
    }

    public String returnRace() {
        if (randomRaces.isEmpty()) {
            randomRaces.addAll(Arrays.asList(masterRaces));
            Collections.shuffle(randomRaces);
        }
        return randomRaces.remove(0);
    }

    public String returnWeapon() {
        if (randomWeapons.isEmpty()) {
            randomWeapons.addAll(Arrays.asList(masterWeapons));
            Collections.shuffle(randomWeapons);
        }
        return randomWeapons.remove(0);
    }

This guarantees that values from the List will only begin to repeat once you have used ALL the values from the original List

We could then encapsulate the concept into a class to make it easier, for example...

public class RandomList<Value> {
    private List<Value> masterValues;
    private List<Value> randomValues;
    
    public RandomList(Value[] masterValues) {
        this(Arrays.asList(masterValues));
    }

    public RandomList(List<Value> masterValues) {
        this.masterValues = masterValues;
        randomValues = new ArrayList<>(masterValues);
        Collections.shuffle(randomValues);
    }
    
    public Value next() {
        if (randomValues.isEmpty()) {
            randomValues.addAll(masterValues);
            Collections.shuffle(randomValues);
        }
        return randomValues.remove(0);
    }
    
    public Value nextOrNull() {
        if (randomValues.isEmpty()) {
            return null;
        }
        return randomValues.remove(0);
    }
}

Which would change your code to something like...

    private RandomList<String> randomStyles = new RandomList<>(new String [] {"<html><p align='center'>Steampunk</p></html>", "<html><p align='center'>Sci-Fi</p></html>", "<html><p align='center'>Medieval</p></html>", "<html><p align='center'>Imprisoned</p></html>", "<html><p align='center'>Superpowered</p></html>", "<html><p align='center'>Villainous</p></html>", "<html><p align='center'>Primal</p></html>", "<html><p align='center'>Musical</p></html>", "<html><p align='center'>Aquatic</p></html>", "<html><p align='center'>Undead</p></html>"});
    private RandomList<String> randomRaces = new RandomList<>(new String [] {"<html><p align='center'>Sorcerer</p></html>", "<html><p align='center'>Alien</p></html>", "<html><p align='center'>Knight</p></html>", "<html><p align='center'>Orc</p></html>", "<html><p align='center'>Elf</p></html>", "<html><p align='center'>Bear</p></html>", "<html><p align='center'>Human</p></html>", "<html><p align='center'>Vampire</p></html>", "<html><p align='center'>Werewolf</p></html>", "<html><p align='center'>Dwarf</p></html>"});
    private RandomList<String> randomWeapons = new RandomList<>(new String [] {"<html><p align='center'>Bow<br />And Arrow</p></html>", "<html><p align='center'>Blade</p></html>", "<html><p align='center'>Gun</p></html>", "<html><p align='center'>Nunchucks</p></html>", "<html><p align='center'>Shield</p></html>", "<html><p align='center'>Bomb</p></html>", "<html><p align='center'>Claws</p></html>", "<html><p align='center'>Mace</p></html>", "<html><p align='center'>Axe</p></html>", "<html><p align='center'>Staff</p></html>"});

and your returnXxx methods to something like...

    public String returnStyle() {
        return randomStyles.next();
    }

    public String returnRace() {
        return randomRaces.next();
    }

    public String returnWeapon() {
        return randomWeapons.next();
    }

which is much simpler and cleaner and removes all the duplicate code ... but, this might be a bit more "advanced" then you are right now, but something to keep in mind ????

下壹個目標 2025-02-03 10:11:58

基本上您可以做这样的事情:

ArrayList<String> fruits = new ArrayList<>(Arrays.asList("banana", "apple", "orange","grape","watermelon"));
Random r = new Random();
while(fruits.size()>0) {
   int random = r.nextInt(fruits.size());
   System.out.println(fruits.get(randomInt));
   fruits.remove(randomInt);
}

创建一个列表。
生成一个随机索引,将元素弹出此索引,重复。

Basically you can do something like this:

ArrayList<String> fruits = new ArrayList<>(Arrays.asList("banana", "apple", "orange","grape","watermelon"));
Random r = new Random();
while(fruits.size()>0) {
   int random = r.nextInt(fruits.size());
   System.out.println(fruits.get(randomInt));
   fruits.remove(randomInt);
}

create a list.
generate a random index, pop the element in this index, repeat.

初见 2025-02-03 10:11:58

如果您希望它能够重复结果,而不是它产生的最后一个结果,则可以存储其产生的最后一个结果,并检查新的结果是否相同。如果是,请生成另一个。以ATP的答案为例并修改以实现此目的,如下所示:

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class RandomGenerator
{
    public static void main (String[] args) throws java.lang.Exception
    {
        ArrayList<String> fruits = new ArrayList<>(Arrays.asList("banana", "apple", "orange","grape","watermelon"));
        Random r = new Random();
        int previousRandom = -1;
        int random = r.nextInt(fruits.size());
        System.out.println("double size: " + 2 * fruits.size());
        for (int i = 0; i < 2 * fruits.size(); i++) {
            if (random == previousRandom) {
                random = r.nextInt(fruits.size());
                i = i -1;
            } else {
                System.out.println(fruits.get(random));
                previousRandom = random;
            }
        }
    }
}   

如果您不希望它重复任何结果,并且仅生成尽可能多的结果,那么ATP的答案将执行诡计。

If you want it to be able to repeat results, just not the last one it produced, you can store the last result it produced and check if the new is the same. If it is, generate another one. Taking the example from ATP's answer and modifying it to accomplish this, it would look as follows:

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class RandomGenerator
{
    public static void main (String[] args) throws java.lang.Exception
    {
        ArrayList<String> fruits = new ArrayList<>(Arrays.asList("banana", "apple", "orange","grape","watermelon"));
        Random r = new Random();
        int previousRandom = -1;
        int random = r.nextInt(fruits.size());
        System.out.println("double size: " + 2 * fruits.size());
        for (int i = 0; i < 2 * fruits.size(); i++) {
            if (random == previousRandom) {
                random = r.nextInt(fruits.size());
                i = i -1;
            } else {
                System.out.println(fruits.get(random));
                previousRandom = random;
            }
        }
    }
}   

If you don't want it to ever repeat any result, and generate only as many results as possible values you have, then ATP's answer will do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文