随机生成器

时间:2016-07-08 21:55:21

标签: android

我正在创建一个随机引用生成器,但我有一个问题,即引号经常重复是为了使它们不重复?

这是我的MainActivity,提前感谢您的帮助

public class MainActivity extends AppCompatActivity {

    private Button addButton;    
    private TextView displayMessageTextView;
    private ArrayList<String> quotes = new ArrayList<String>();
    private Random randomGenerator = new Random();
    private int previousNumber = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        quotes.add("You smart! You loyal! You're a genious!");
        quotes.add("I appreciate you");
        quotes.add("This is a major key");
        quotes.add("They will try to close the door on U, just open it");
        quotes.add("The key to more success is CoCo butter");
        quotes.add("Congratulations, you played yourself");
        quotes.add("Another one, no. Another two");
        quotes.add("I changed... a lot");
        quotes.add("They don't want you to jet ski");
        quotes.add("The key is to have every key");
        quotes.add("Almond milk + Cinnamon Toast Crunch = Major key to success");
        quotes.add("Do you see that bamboo? Ain't nothing like bamboo. Blessup");
        quotes.add("Bless up. Egg whites, Turkey bacon, Hashbrown, Water");
        quotes.add("They wanna come stress me out? Heh, bye");
        quotes.add("Lion Order");
        quotes.add("Watch your back, but when you get out of the shower dry your back, it's a cold world");
        quotes.add("Some of ya'll crabs");
        quotes.add("ANOTHA ONE");
        quotes.add("We jus seen 2 dolphins");
        quotes.add("They don't want you to win");
        quotes.add("Be A Star. Be A Superstar");
        quotes.add("I remember when I ain't have a jacuzzi");
        quotes.add("The other day the grass was brown, now it's green cuz I ain't give up");
        quotes.add("In life everyone has a choice. The key is...make a right choice");
        quotes.add("We have to get money. We have no choice. It cost money to eat");
        quotes.add("I love my bamboo trees. I love fruits. I love apples.");
        quotes.add("I told y'all this before, when you have a swimming pool do not use chlorine, use salt water");
        quotes.add("The key is: never fold");
        quotes.add("major key, get a pedicure and manicure once a week");
        quotes.add("They dont want you to be healthy");
        quotes.add("To make it thru the jungle you're gonna have to sweat");
        quotes.add("They never said winning was easy");
        quotes.add("It's important to shape up your hedges. It's like getting a haircut");
        quotes.add("LIOOOOON");
        quotes.add("To succeed, you must believe. When you believe you will succeed. ");
        quotes.add("The key to success is to have a hammock");
        quotes.add("Some people can't handle winning. I can.");
        quotes.add("They don't want you to have lunch");
        quotes.add("It's not an easy road but give thanks to the road");
        quotes.add("The key to success is to have a lot of pillows. A lot.");

        displayMessageTextView = (TextView) findViewById(R.id.displayMessageTextView);
        displayMessageTextView.setText("Press the button to generate a random quote");

        addButton = (Button) findViewById(R.id.addObjecttive);
        addButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int randomQuoteIndex = getRandomNumber();

                String quote = quotes.get(randomQuoteIndex);

                displayMessageTextView.setText(quote);
            }
        });
    }

    public int getRandomNumber() {
        int randomNumber = randomGenerator.nextInt(40);

        while (previousNumber == randomNumber) {
            randomNumber = randomGenerator.nextInt(40);
        }

        previousNumber = randomNumber;

        return randomNumber;
    }
}

2 个答案:

答案 0 :(得分:0)

在stackoverflow上有很多关于这个主题的讨论:

Creating random numbers with no duplicates

在您的情况下,以下方法也可能有效:

  • 创建数组引号的副本,例如quotesCopy
  • 创建0到39之间的随机数
  • 从quotesCopy中选择一项并将其删除
  • 创建0到38之间的随机数
  • ...
  • 如果quotesCopy变空,
  • 从头开始

编辑:

Code cood看起来像这样:

String returnRandomQuote(){
    if(quotesCopy==null || quotesCopy.size()==0){
        quotesCopy = (ArrayList<String>) quotes.clone();
    }
    Collections.shuffle(quotesCopy);
    String ret = quotesCopy.get(0);
    quotesCopy.remove(0);
    return ret;
}

答案 1 :(得分:0)

我想到的第一件事就是创建该列表的副本并删除您显示的每个引用。当cpied数组的长度小于某个硬编码值时,你将重新复制原始arrray。

第二个实现相同的工作是将每个选择的引用复制到某个数组,并在每个随机获取后检查它是否存在于此数组中,如果是,则尝试获取另一个。如果该数组的长度将大于某些硬编码值而不是刷新它。

编辑:

您必须创建另一个ArrayList。然后当你接受任何引用时,你检查它是否存在(当然第一个不存在,因为它是空数组)。如果它不存在,则将其添加到副本数组中。所以它不会在短时间内被选中。当保存(显示)的引号数重现一些值(此处为20)时,您清空此数组,以便随机选择可以再次启动将原始列表。如果修改了下面的代码。我不是Java程序员,但应该可以工作。

public class MainActivity extends AppCompatActivity {

    private Button addButton;    
private TextView displayMessageTextView;
private ArrayList<String> quotes = new ArrayList<String>();
private ArrayList<String> quotes = new ArrayList<String>();
private Random randomGenerator = new Random();
private int previousNumber = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    quotes.add("You smart! You loyal! You're a genious!");
    quotes.add("I appreciate you");
    quotes.add("This is a major key");
    quotes.add("They will try to close the door on U, just open it");
    quotes.add("The key to more success is CoCo butter");
    quotes.add("Congratulations, you played yourself");
    quotes.add("Another one, no. Another two");
    quotes.add("I changed... a lot");
    quotes.add("They don't want you to jet ski");
    quotes.add("The key is to have every key");
    quotes.add("Almond milk + Cinnamon Toast Crunch = Major key to success");
    quotes.add("Do you see that bamboo? Ain't nothing like bamboo. Blessup");
    quotes.add("Bless up. Egg whites, Turkey bacon, Hashbrown, Water");
    quotes.add("They wanna come stress me out? Heh, bye");
    quotes.add("Lion Order");
    quotes.add("Watch your back, but when you get out of the shower dry your back, it's a cold world");
    quotes.add("Some of ya'll crabs");
    quotes.add("ANOTHA ONE");
    quotes.add("We jus seen 2 dolphins");
    quotes.add("They don't want you to win");
    quotes.add("Be A Star. Be A Superstar");
    quotes.add("I remember when I ain't have a jacuzzi");
    quotes.add("The other day the grass was brown, now it's green cuz I ain't give up");
    quotes.add("In life everyone has a choice. The key is...make a right choice");
    quotes.add("We have to get money. We have no choice. It cost money to eat");
    quotes.add("I love my bamboo trees. I love fruits. I love apples.");
    quotes.add("I told y'all this before, when you have a swimming pool do not use chlorine, use salt water");
    quotes.add("The key is: never fold");
    quotes.add("major key, get a pedicure and manicure once a week");
    quotes.add("They dont want you to be healthy");
    quotes.add("To make it thru the jungle you're gonna have to sweat");
    quotes.add("They never said winning was easy");
    quotes.add("It's important to shape up your hedges. It's like getting a haircut");
    quotes.add("LIOOOOON");
    quotes.add("To succeed, you must believe. When you believe you will succeed. ");
    quotes.add("The key to success is to have a hammock");
    quotes.add("Some people can't handle winning. I can.");
    quotes.add("They don't want you to have lunch");
    quotes.add("It's not an easy road but give thanks to the road");
    quotes.add("The key to success is to have a lot of pillows. A lot.");

    displayMessageTextView = (TextView) findViewById(R.id.displayMessageTextView);
    displayMessageTextView.setText("Press the button to generate a random quote");

    addButton = (Button) findViewById(R.id.addObjecttive);
    addButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Boolean isDifferent = false;
            while(!isDifferent)
            {
                int randomQuoteIndex = getRandomNumber();
                String quote = quotes.get(randomQuoteIndex);
                if(!quotesCopy.contains(quote)) {
                    isDifferent = true;
                    quotesCopy.add(quote);
                }
                if(quotesCopy.size() > 20)
                    quotesCopy = new ArrayList<String>();
            }

            displayMessageTextView.setText(quote);
        }
    });
}

public int getRandomNumber() {
    int randomNumber = randomGenerator.nextInt(40);

    while (previousNumber == randomNumber) {
        randomNumber = randomGenerator.nextInt(40);
    }

    previousNumber = randomNumber;

    return randomNumber;
}

}