0

Im working on the Exercises Arrays and ArrayLists form the book Java How to program by Deitel 9th edition. Here's is the code followed by my questions

public static void main(String[] args) {
    // TODO code application logic here
    Random r = new Random();

    int[][] sales = new int[5][4];

    // display salesnames
    System.out.println("\t\t  1.Tom   2.Eva   3.Jan   3.Roy    Total);     

    // declare ProductCounter
    int proCount = 1;

    // display array
    for (int row = 0; row < sales.length; row++) {
        System.out.print("Product " + proCount + "\t");

        for (int column = 0; column < sales[row].length; column++) {
            sales[row][column] = 0 + r.nextInt(2);
            System.out.printf("  %d\t", sales[row][column]);

        }
        proCount++;
        System.out.println();
    }

}

}

After the second for loop i fill the array with random numbers 0 or 1 and the result looks like this:

                  1.Tom   2.Eva   2.Jan   4.Roy    Product Totals
   Product 1      1       0       1       0 
   Product 2      0       0       1       1 
   Product 3      0       1       0       1 
   Product 4      0       0       0       1 
   Product 5      0       0       0       0 

Question: Each salesperson passes in 0-5 sales slips per day

Problem: This random only randoms one time per product. How do I code so it randoms between 0-5 so that could also mean that it could be 3 of product number2 because now it only decides if one product is sold one time or not.

2
  • Look at the javadoc of Random's .nextInt() method which you use; this should lead you to the answer ;) Commented Feb 4, 2015 at 2:12
  • It's pretty unclear what you asking - you will need to clarify. I think what you are asking is how to generate 5 random numbers that add up to 5 or less. Is that correct? Commented Feb 4, 2015 at 2:53

5 Answers 5

1

nextInt takes a parameter which defines the upper bound of the random number generated.

Sign up to request clarification or add additional context in comments.

1 Comment

@JamesMassey Care to clarify how my answer to the question isn't an answer to the question? I didn't ask for clarification. I was instructing him towards the answer.
0

Your question is really not asked in a very clear way.

From what I understand, you want:

  1. A sales to be able to have 0-5 sales
  2. You want to randomly distribute his sales among 5 products

Then here is the pseudo-code that you can do:

// code that deal with only 1 sale
int noOfSales = get a 0-5 random number;
loop for noOfSales times {
    p = get a random number less than totalNumberOfProducts
    // to decide which product to sell

    product[p] += 1;
}

1 Comment

I'm really sorry about this confusing question. I think I can solve it with this pseudo-code.
0
sales[row][column] = 0 + r.nextInt(6);

This line of code will allow you generating a random Integer (0~5).

2 Comments

Yes, that I understand but then a salesperson sales is between 0-25. One sales person laves in 0-5 sales slips per day.
Sorry, I really dont understand what you said and ur problem. Would be easier if you show some specific cases.
0
              1.Tom   2.Eva   3.Jan   3.Roy    Product Total
Product 1     1       0       3       3 
Product 2     2       0       0       5 
Product 3     5       0       5       1 
Product 4     4       4       3       0 
Product 5     1       3       3       1 

This how it looks if i do with r.nextInt(6);

The total sales for one person needs to be 5.

Comments

0

I'm guessing that your question is actually "how can I generate 5 random numbers that can each be 0-5 but that add up to no more than 5". Please confirm if that's what you mean in the comments.

But if it is what you mean then it's impossible to answer without more information about how you need the random numbers to be distributed. For example an easy answer is:

int numbers[5];
int total = Random.nextInt(6);
for (int i = 0; i < numbers.length && total > 0; i++) {
    numbers[i] = Random.nextInt(total);
    total -= numbers[i];
}

However I doubt very much that is what you want as it will heavily skew the larger numbers to the front of the list. So you will need to decide what sort of distribution of randomness you want before we can give further guidance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.