aspose file tools*
The moose likes Beginning Java and the fly likes Java Random 4 digit number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Random 4 digit number" Watch "Java Random 4 digit number" New topic
Author

Java Random 4 digit number

Anudeep Duvvuri
Greenhorn

Joined: May 18, 2012
Posts: 29
How to get a random 4 digit number without repetition I mean the random number should be 1234 or 4576 but not 1223 or 1244 like this. here in 1233 3 is repeated twice i don't want those numbers

Please anyone help me its very urgent
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

Anudeep Duvvuri wrote:How to get a random 4 digit number without repetition...
Please anyone help me its very urgent

Please EaseUp (←click). Urgency is your problem, not ours, and we're all volunteers here.

My suggestion: Make an array of single digits, shuffle it, and take the first 4 (or 5, if the first one is '0').

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
That is not a random number. Try random selection from a declining population with a List of Integers [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] and a Random object to get the index.


And we only use the word “urgent” on 1st April.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Campbell Ritchie wrote:Try random selection from a declining population with a List of Integers [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] and a Random object to get the index.

I don't see much difference between this and Winston's suggestion to shuffle and take the first four or five. Shuffle is going to randomize the order.


Junilu - [How to Ask Questions] [How to Answer Questions]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

Junilu Lacar wrote:I don't see much difference between this and Winston's suggestion to shuffle and take the first four or five. Shuffle is going to randomize the order.

I suspect Campbell and I were both typing at the same time; but by some miracle I happened to get in first.

Winston
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

Winston Gutkowski wrote:My suggestion: Make an array of single digits, shuffle it, and take the first 4 (or 5, if the first one is '0').

That would depend on the specific requirements. It is possible that 0837 is a valid output.

Anudeep Duvvuri, you need to be specific in what you want - what is and what isn't allowed. This is the single most important thing all developers need to learn - how to write clear, complete specs.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Winston Gutkowski wrote:I suspect Campbell and I were both typing at the same time; but by some miracle I happened to get in first.

Got it. Reading it again, I realize that Campbell's "That is not a random number" comment was directed at the OP, not at your reply.
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
This is NOT the best way to do this. However, it shows some of the qualities of a set and how one might go about making one. A simple ArrayList shuffle is the least amount of code but also gives you the least amount of control. The previous answers would/should be sufficient for your needs. This is just my 2 cents.



EDIT: The fun part of this code is the halting problem applies.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Anudeep - when you say "without repetition", what about 1232 or 1241? Does it count as "repetition" if the repeated digits are not adjacent to each other? The algorithms given thus far do not allow any repetition at all, even non-adjacent repetition. But your post isn't entirely clear on this point, so this might be something worth considering.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
I was away enjoying the fresh air and fireworks at Whitby, but people worked out the correct history without my help
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

Rooks Forgenal wrote:A simple ArrayList shuffle is the least amount of code but also gives you the least amount of control.

Can you explain that? I'm not saying that one has to do a shuffle, but whenever I hear the words 'random' and either 'unique' or 'distinct' used together, it's what immediately springs to my mind.
1. It's execution time is predictable and constant.
2. It works for choosing any number of objects from 2 to n from a set of size n.
3. It gets more efficient the more objects you need to choose; the "pick a random object I haven't picked yet" method gets less efficient.
4 It is likely to be the "most" random form of such a selection for a given RNG (Random Number Generator).

The reason for #4 is that the 'shuffle' algorithm itself uses the RNG exactly once for each element, and can actually be proved to be as random as possible, given the constraints of a shuffle. Your method is more like a cache, where you have 'hits' and 'misses'; and it's the misses that may highlight any skews in your RNG.

Oddly enough, you don't even need to shuffle the entire list/array in order to select a bunch of objects, viz:Note: the above method will obviously change the contents of data; but it can be called repeatedly with no loss of randomness. It's basically a partial Fisher-Yates shuffle.

Winston
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
I LOVE that code. I love it because I can predict its runtime it is simple and short.

You want me to explain the sentence I wrote?


short and simple. ...but not very malleable.
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Let's make it generic, shall we?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

Rooks Forgenal wrote:Let's make it generic, shall we?

If you want to make it really generic, how about:The only method you'd need to add is:
public T remove()
which removes an item at random from the Bag. Sound familiar?

Winston
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Hmmm, that remove() sounds deceptively similar to an existing API method, but isn't.

I was thinking of something more like

This way you don't need to spend time copying the original source, and you don't change the source. You can just internally create an int[] for all the indices, lazily shuffle them (even lazily initializing them if you want), and use the shuffled indices to view the source.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
For the earlier generic code, I would at least make it a generic method, not a method on a generic class. I.e. change

to

The type can be inferred from both srcArray and TClass, no need to get it from the parent class as well. And if we use collections instead of arrays, we don't need TClass either - we can infer the whole thing. Which leads to the API in my previous post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Random 4 digit number