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 8982281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:32:29+00:00 2026-06-15T20:32:29+00:00

I want to generate random numbers within the range 1 to 4, 4 including.

  • 0

I want to generate random numbers within the range 1 to 4, 4 including.
Here is my code:

int num = r.nextInt(4) + 1;  //r is instance of Random.

However, I am running the above code in a loop and don’t want repeating random number.
What happens now is often I am getting:
1,1,1,2,3,1,4,2,2,1,4,2,4,4,2,1,4,3,3,1,4,2,4,1 as my output.

Here, though the numbers are random within the range(1-4), but often repeated like the number “1”in the first 3 iterations.

What I am looking for is a way to get non repeating random number within the loop.
One simple way I know is of keeping the last random number before current iteration and compare, but I am sure there must be better solution to this.
Thanks in advance.

  • 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-15T20:32:31+00:00Added an answer on June 15, 2026 at 8:32 pm

    Use random.nextInt(range-1) and then map that number to the output number with a function that excludes the previous number:

    public class Test {
      private final Random random = new Random();
      private final int range;
      private int previous;
    
      Test(int range) { this.range = range; }
    
      int nextRnd() {
        if (previous == 0) return previous = random.nextInt(range) + 1;
        final int rnd = random.nextInt(range-1) + 1;
        return previous = (rnd < previous? rnd : rnd + 1);
      }
    
    
      public static void main(String[] args) {
        final Test t = new Test(4);
        for (int i = 0; i < 100; i++) System.out.println(t.nextRnd());
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate random numbers within a range and the generated numbers should
I want to generate random numbers with a range (n to m, eg 100
I want to generate random numbers in the range 1-10. The requirement is that
i want to generate an array of random numbers for example if the range
I need to generate random UNIQUE numbers within a range, how can I do
I want to generate random numbers in the range -1, 1 and want each
Possible Duplicate: Generate Random numbers uniformly over entire range I want to generate the
i want to generate a sequence of unique random numbers in the range of
I want to generate a list of random numbers, where the range of each
So here is the deal: I want to (for example) generate 4 pseudo-random numbers,

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.