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').
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 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 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.
EDIT: The fun part of this code is the halting problem applies.
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.
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.
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.