I'm making a bingo like program in java, I was wondering if it was a ll possible to select a number from a pool, then have it cross it out. I was thinking of putting the 75 (bingo numbers) into an array then have it select it from there, but i cant seem to find a way to get rid of that number once it's selected. for example, i only want to call the number 55 ONCE, then have it gone, or non-accessible from the pool once it's called by my random function.
Thanks
Rob
Generate array 1..75.
Shuffle.
Read one at the time.
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#shuffle%28java.util.List%29
(deleted my previous answer because I misread the question)
Easiest way I can think of to do this is to store them in an ArrayList, track the size and feed that into a random number generator to randomly access an index and remove after use.
Place all 75 numbers into an array.
Call Arrays.shuffle() on the array.
Read the array in-order.
Create a Collection of Integers
Randomly generate an int with a range from zero to collection.size()-1
Remove the item at the index of the random int from step 2. This item is the number you call and will no longer be selectable.
Related
I am trying to print the contents of an array. For example I have defined an int array of size 10. But the user entered only 8 numbers. So the last two positions in an array have an value of zero. When I am printing the array I get all ten positions. Is it possible to print only up till the user entered.Also the user decides how many to enter so I cannot hard code the position for printing the array. Thanks.
Is it possible to print only up till the user entered.
Yes, keep track of how many items the user entered. This can be done with a separate int counter variable that you increment as the user enters an item, or if you want, you can fill the array with values that the user would never enter, for example, Integer.MIN_VALUE, and then display results up until you reach the non-sense values. The danger here is, what happens if the user just happens to pick that non-sense value? That's why I'd go with the first suggestion.
Also the user decides how many to enter so I cannot hard code the position for printing the array.
Yep, just as I said above.
Edit: or best of all, just do what Patricia Shanahan says.
If you have to use an array, keep track of the number of valid elements as suggested by Hovercraft Full Of Eels' answer.
If you have the option, it may be cleaner to use an ArrayList, which grows as you add elements. That way, at the end of input it will have exactly the right size, and you can print the whole of it.
I am curious if anyone knows a way to use math.random to generate random numbers between say 0 and 3, But when it generates two 0's or two 1's it rules out the possibility of generating them numbers?
This is for a game assignment for college and all I have left to do is set it that it only generates two of each number with one 3. If anyone knows how this would be very helpful (even if it is using something other than math.random.
The language is Java.
So, you basically want to keep track of the number draws, right?
A possible solution is to use an array whose length is the valid range of your random numbers, where each cell counts the occurrences of the respective number. Then, for each draw, you check the contents of the respective cell in the array to see if you reached the limit. If you did, then redraw and repeat.
Note: at the time of writing this, the language of use is unknown, but the solution is generic enough to be implemented in virtually any language that provide a random() function.
I have a array shuffled with some numbers I want to randomize the numbers in shuffled array without duplication am using below code
int shuffled array [] = {1,3,5,7,8}
Random random = new Random();
int random =shuffled array[random.nextInt(shuffled array.length)];
I am getting duplicate values.
How do I get to generate random number without any duplicate values?
In general you can do in of the following:
Create random numbers and keep them in a Set so that they are unique and keep pooling them until the desired amount of random numbers is achieved.
If you have a pool by which you want to pool numbers you can create an array (as you do) and then shuffle this array. Why this solution does not work for you? I think you are mixing the 2 solution and get duplicates.
Edit:
About efficiency (if you are concerned about it):
The first method uses a pseudorandom generator to produce the unique numbers. As the number of unique number in respect to the total possible numbers increases, (e.g. if you have an array and want all element to be picked up at random) then this approach might be inappropriate. If on the other hand you just want to pick 10 unique random numbers it is possibly efficient enough.
The shuffle approach as states This method runs in linear time so it should be preferred in this case (yours that is).
int pos =shuffled array[random.nextInt(4)]
int random_num_from_shuffled_array = shuffled array[pos];
Create a Collection (Set - because of the no-duplicates requirement) using your array.
Use Collections.shuffle(mySet);
You are actually using the Random class and nextInt which do not guarantee uniqueness of consecutive elements!
I guess what you need is achieved by Collections.shuffle. See the documentation
I know about the JAVA Random class and its use. But I want to generate a random number which should not be repeated until all the numbers in the range are generated at least once. Can anybody provide me some reference..?
While using the Random class, the problem I face is that some numbers get repeated 2 or 3 times while some are not generated at all.. My application may fail in this scenario as I have another thread processing some requests based on the numbers getting generated....and the moment there is a delay and next unique digit is not available, it stops without processing the non - generated numbers...
You can generate all the values in range and shuffle them. Collections.shuffle() Once you have uses every value, repeat.
Imagine you have decks of cards, You take one deck which has a every card once, you shuffle it and you will know what while each card will come in a random order, only once. When one deck has finished you take all the cards again and reshuffle (or a new deck and shuffle that)
Create a List with all the possible random numbers in the range.
Instead of using a random number as result, use a random integer to pick an index in the list
remove it from the list and return it.
Take care to adjust the range of random integer to the current list size (it will decrease by one each time you get a new number)
you can keep a list with you of all the numbers that are already picked out and check if the new random number is in that list or not
Here is an example of someone who asked the same question
I am writing an Android app and I want to generate random numbers.
But, Java's RandomGenerator gives me only pseudo random numbers. The numbers repeat and not all the numbers are covered.
I want something that will give me non-repeating numbers and will cover all the numbers. How do I do that?
You can put all random values you want into a List and shuffle it.
List<Integer> numbers = ...
Collections.shuffle(numbers);
This will give you unique numbers in a random order.
You could fill a data structure with the numbers you want to loop over, then randomize the order of the elements in the structure and pull them out one by one. Alternatively, you could randomly pick indexes, and retrieve elements at those indexes. Whichever you do (you would choose the one more efficient for the specific data structure), you be sure to remove this element as you grab it. As you keep going your data structure will get smaller and smaller until you've received every element and have nothing left. This also ensures you'll never hit the same number twice, because you'll have removed it from your pool of possible numbers.