-4

This my Button OnClick() Method on Click of the button All Four random numbers will be Displayed on the Logcat

public void onClick(View v) {

            /*  the Code for Four Random Numbers*/

            final Random random = new Random();

            final Set<Integer> mySet = new HashSet<>();

            while (mySet.size() < 4) {

                mySet.add(random.nextInt(69) + 1);
            }

            // Now Adding it to the ArrayList                

           ArrayList<Integer> Elements =  new ArrayList<>(mySet);
            Log.i("Elements","A:" + Elements.get(0));
            Log.i("Elements","B:" + Elements.get(1));
            Log.i("Elements","C:" + Elements.get(2));
            Log.i("Elements","D:" + Elements.get(3));

        }
    });

The Output Will be Look Like this (I just give an Example of one case It is Different in every case Whenever I run a App) A : 11 B : 28 C : 57 D : 1

Now the Problem is : The sum of all the numbers is greater than the specified range which is 0 to 69

When We Add A,B,C,D values which is equals to 97

Which is greater than the Specified Range 0 to 69

So I Want the Random Numbers in such a way that :

when we Add A,B,C,D the their Sum Should no Exceed the Range that is 69

So My Question is How can i Do That ? Please Help!! I am Stuck in that part of the Code and I find no Solution

4 Answers4

1

In addition to what other people have suggested, you might want to read this: find all subsets that sum to a particular value

First, note that this bears some resemblance to the subset sum problem. Given the set of all numbers between 1 and 69, you're looking for a subset of 4 of them that adds up to 69. For any natural number, there are only a finite number of such sets (although it obviously eventually gets computationally infeasible to enumerate all of them). Either way, your answer is guaranteed to be one of these sets regardless of what algorithm you use.

The linked question shows code to find all of the subsets that add up to a particular value. Once you have this, just filter on all the subsets that have length 4 and randomly pick one of them.

  • Good respoonse! Though there is really infinite number of integers that have a sum between 0-69 unless a sample space is specified. – Martin Sep 08 '18 at 07:51
  • @Martin Good point - I will admit I slightly misread the question and assumed that it was referring to numbers in a particular range that still add up to a number in that range. I agree that you'd need that constraint for this algorithm to work. – EJoshuaS - Stand with Ukraine Sep 08 '18 at 13:41
0

There are two ways, depending on how you want to do it:

  1. Rereoll the random numbers until a valid sum is achieved. This will be truly random.

  2. Change the max value for the 2nd, 3rd, 4th, etc random number so that it can't be too big. However this will change it from every number being equally likely to smaller numbers being more likely.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Another approach is to find all the subsets that sum to 69 and then randomly select one. It's computationally very expensive, but it demonstrates the most random behavior. – EJoshuaS - Stand with Ukraine Sep 07 '18 at 18:51
  • @EJoshuaS Randomness would be equal to my approach 1, but it would finish in bounded time, and the computation of the choices could be preloaded if this is called frequently. My way does have the possibility of unbounded time if you get unlucky. Either are valid approaches. – Gabe Sechan Sep 07 '18 at 18:53
-1

This is a tricky question. These is no mention of a sample space, so one can assume it has to be valid integer of sorts. Generate any number of a number signed 32 bit integer. This would be –2147483648 and 2147483647 until you have four unique ones which sum is between 0 and 69.

Best of luck!

Martin
  • 619
  • 5
  • 13
-2

you can use this code :

        final Random random = new Random();
        final Set<Integer> mySet = new HashSet<>();
        int thesum = 0 ;

        while (mySet.size() < 4  && thesum< 69) {
            int nmbr = random.nextInt(69) + 1 ;
            thesum = thesum + nmbr ;
            mySet.add(nmbr) ;     
        }

       if( mySet.size() == 3)
         {ArrayList<Integer> Elements =  new ArrayList<>(mySet);
        Log.i("Elements","A:" + Elements.get(0));
        Log.i("Elements","B:" + Elements.get(1));
        Log.i("Elements","C:" + Elements.get(2));
        Log.i("Elements","D:" + Elements.get(3));}