简体   繁体   中英

I want to generate four Different Random Numbers in android But Their total should not be Greater Than the Specified range like 0 to 69

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

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.

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.

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!

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));}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM