Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8607557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:23:08+00:00 2026-06-12T03:23:08+00:00

I have a question regarding the following code Random randomGenerator = new Random(); int

  • 0

I have a question regarding the following code

Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(4);
    String wordList[] = new String[4];
    {
        wordList[0] = "Red";
        wordList[1] = "Blue";
        wordList[2] = "Green";
        wordList[3] = "Orange";


    }

String wordToDisplay = wordList[randomInt];

This code works fine however I would like to know if it was possible to get it to not pick the same word two times in a row. For example, if it just selected “Red” then it would not pick “Red” again the next consecutive time. I read something about DISTINCT but I’m not sure if that’s along the right path.

Here is the code for the button which uses this

final Button button1 = (Button) findViewById(R.id.button1);
        final TextView textView = (TextView) findViewById(R.id.text_random_text);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                Random randomGenerator = new Random();
                int randomInt = randomGenerator.nextInt(9);
                String wordToDisplay = wordList[randomInt];
                textView.setText(wordToDisplay);

Thankyou for your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:23:10+00:00Added an answer on June 12, 2026 at 3:23 am

    go for list and remove color once used:

    private static ArrayList<String> arrayList = new ArrayList<String>();
    private static Random random = new Random();
    public static void fillList(){
        arrayList.add("Red");
        arrayList.add("Blue");
        arrayList.add("Green");
        arrayList.add("Orange");
    }
    public static String getNextRandomColor(){
        if(arrayList.isEmpty()){
            fillList();
        }
        return arrayList.remove(random.nextInt(arrayList.size()));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding the following code snippet I came across in one
I am new to chef and I have a question regarding the following. I
another noob question regarding F#. If I have the following code... let ExeC =
I have a question regarding the following code: while((c = getc(pFile)) != EOF) {
I have a question regarding placement new syntax in C++. Are the following two
I have a question regarding string modification. Let's assume that we have the following
Quick question regarding EventHandlers in C#, let's say we have the following code: MyObject.MyEventHandler
I have a question regarding exception handling. Consider following Java code snippet. try{ //code
I have a question regarding memory allocation order. In the following code I allocate
I have the following question regarding the code below: public class GenericBridgeMethods <T> {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.