Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Generating random 3 digit kength number with specific numbers. Need help!

  1. #1
    Junior Member
    Join Date
    May 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Generating random 3 digit kength number with specific numbers. Need help!

    hi all.
    My question is what can I sue to create String with random 3 digit length number (let's say ID) but made of only specific numbers like 1 3 6 9 0. Can use here math.random ? or is there another way, easier way. Can somepone please enlighten me here?

  2. #2
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: Generating random 3 digit kength number with specific numbers. Need help!

    public static String RandomString(int[] digits, int length) {
    Random r = new Random();
    String str = "";
    for(int i = 0; i < length; i++) {
    int n = (int) (r.nextDouble()*digits.length);
    str += digits[n];
    }
    return str;
    }

Similar Threads

  1. Generating a random number from a range of inputted numbers
    By R0bert199O in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 01:48 PM
  2. Generating random numbers in java without repeating a specific number
    By ojonugwa in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 26th, 2013, 12:42 AM
  3. Replies: 5
    Last Post: November 29th, 2012, 01:25 PM
  4. Generating random numbers
    By abelned in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2010, 07:24 AM
  5. Generating random letters and numbers together
    By newJava in forum Java Theory & Questions
    Replies: 3
    Last Post: March 19th, 2010, 04:08 AM