---
title: "Java random number with given length"  
description: "Java random number with given length"  
author: "Anonymous User"  
published: 2013-05-07  
updated: 2013-05-07  
canonical: https://www.mindstick.com/forum/830/java-random-number-with-given-length  
category: "mssql server"  
tags: ["mssql server"]  
reading_time: 2 minutes  

---

# Java random number with given length

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances)!\
I need to genarate a [random](https://www.mindstick.com/forum/33419/generating-random-numbers-in-objective-c) number with exactly 6 digits in Java. I know i could loop 6 times over a randomicer but is there a nother way to do this in the [standard](https://www.mindstick.com/articles/23223/naming-convention-or-coding-standard) \
Java SE ?\
EDIT: Follow up [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time): Now that I can generate my 6 digits i got a new [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic), the whole ID I'm [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to create is of the syntax 123456-A1B45. So how do i \
randomice the last 5 chars that can be either A-Z or 0-9? I'm [thinking](https://www.mindstick.com/blog/11889/how-to-change-your-thinking-paradigm) of using the char [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) and randomice a number between 48 - 90 and simply drop any value that \
gets the numbers that represent 58-64. Is this the way to go or is there a better solution?\
EDIT 2: This is my final solution. Thanks for all the help guys!\
protected [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) createRandomRegistryId(String handleId){ // syntax we would like to generate is DIA123456-A1B34 String val = "DI"; \
// char (1), random A-Z int ranChar = 65 + (new Random()).nextInt(90-65); char ch = (char)ranChar; val += ch; \
// numbers (6), random 0-9 Random r = new Random(); int numbers = 100000 + (int)(r.nextFloat() * 899900); val += String.valueOf(numbers);\
val += "-"; // char or numbers (5), random 0-9 A-Z for(int i = 0; i<6;){ int ranAny = 48 + (new Random()).nextInt(90-65);\
if(!(57 < ranAny && ranAny<= 65)){ char c = (char)ranAny; val += c; i++; }\
}\
return val;}\
Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by AVADHESH PATEL

Hi Goti Bandhu!\
Generate a number in the range from 100000 to 999999.\
// pseudo codeint n = 100000 + random_float() * 900000;I’m pretty sure you have already read the documentation for e.g. Random and can figure out the rest yourself.\
\


---

Original Source: https://www.mindstick.com/forum/830/java-random-number-with-given-length

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
