lundi 3 avril 2017

How to fill random slots?

I am making a code class which randomly generates a certain amount of slots between 4 and 32. I need to fill said slots, with the integers 1,2,4, or 8. The sum of these integers, must also equal 32. For example, if I randomly generate the number of slots to equal 12, I would need the program to randomly generate a combination of 12 numbers (using 1,2,4 or 8) to fill these slots, while also adding to 32. I have the code to randomly generate the number of slots, it is just the rest I am having trouble with. I am a beginner, so if it is a very simple solution, please do not judge me.

Here is the code for the random slot generator if you want to look at it.

import java.util.Scanner;
import java.util.Random;

public class BELSrmg{
  public static void main (String [] args){
    Scanner k = new Scanner (System.in);
    Random r = new Random ();

    int low = 4;
    int high = 33;
    int result = r.nextInt(high - low) + low;

    System.out.print("here are the number of slots " + result );
  }
}




Aucun commentaire:

Enregistrer un commentaire