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.
Related
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'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.
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
Here's an overview of what I want to do.
The program will accept positive and negative integers to do addition, substraction, multiplication, division.
There will be two jars beside each other, while the numbers are being entered balls will drop in the jar.
If the numbers being entered are positive it will drop in one jar and if the numbers are negative it will drop in the other jar. When the calculation is perform the results of the number of balls will remain in the jar.
Also more than two numbers can be calculated at one time.
It's simple but am not sure what path to take.
Can someone guide me on how to this.
I have started but not sure if am on the right track.
Here's what I did so far.
I have two text field with a drop down to choose the type of operation (x, /, -, +).
An "add" button to add more text field if more numbers are required.
But am not sure how to store those numbers and have the numbers drop the amount of balls as they are enter into the text field.
Any help or ideas would be greatful.
Thanks.
Unless I misunderstand what you are trying to do, it looks like you want to have a sort of visual calculator with a variable number of operations per calculation.
While many may argue this, an easy and educational way of doing this would be to create an "Operation" object (class) that holds the entered number and the operation that will be performed (+-*/).
From here there are two approaches that you can take...
The sane approach:
Limit the number of operations to a sane number (ie. 8) and create a statically sized array ( ie. Operation[] ops = new Operation[8]; ) and for every new operation up to the limit, add it to the array.
Once you do this you can either keep a counter of entries or search through the array until you hit an uninitialized index then sort the operations by precedence (+ then - then * then /...) and run the calculation.
The not-so-sane approach:
Unlimited operations! The same thing as previous only using an ArrayList ( ArrayList, java API ) and once you have the total number of entries, do the same as above.
Happy coding ;)