# How to create random number ?

Page 1 of 1

## 5 Replies - 2225 Views - Last Post: 09 March 2010 - 08:06 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=160748&amp;s=a9cbbbee3c9ad0d4d5c475901805c109&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 biggmann

Reputation: 0
• Posts: 77
• Joined: 02-February 10

# How to create random number ?

Posted 08 March 2010 - 10:57 PM

I am working on a school project so I am just learning and I am stuck on this small part. I am supposed to create an employee and a random employee number and cant figure out how to pick a random number. Here are the instructions givin and then the code I have so far.

Write a program that makes use of a class called Employee that will calculate an employee's weekly paycheque. Design your class based on the UML diagram to the right as well as the following notes:

* A static field, empCount, keeps track of the number of instantiated employees and can be retrieved using the static method getCount( )
* The constructor initializes the employee's name and employee number (a random number in the range of 1000 - 9999)
* The method setEmployeePay( ) is overloaded to accomodate different payment methods based on empType:

o Type 1 - Salaried employees have a yearly salary, payed on a weekly basis
o Type 2 - Hourly employees are paid an hourly rate for the number of hours worked (overtime is time and a half for hours over 40)
o Type 3 - Piece employees are paid a base amount plus \$24.00 for every piece completed

* The method calculatePay( ) will determine what the employee's weekly pay is, based on their payment type
* The methods getName( ) and getNumber( ) return the employee's name and employee number respectively
* All input & output should be displayed in the main class only

The main program should test your class by instantiating four different employees:

* Test a salaried employee
* Test two hourly employees, one who has worked overtime and one who hasn't
* Test a piecework employee
* The following data can be hard-coded into your program (no prompts to enter data other than the names):

o Employee 1: (22.50, 35.0)
o Employee 2: (45350.00)
o Employee 3: (500.00, 25)
o Employee 4: (14.75, 48.0)

```public class Employee {

private String empName; //employee name
private int empNumber;  //employee number
private double empSalary;  //employee salary
private double empRate;  //employee pay rate
private double empHours;  //employee hours worked
private double empBase;  //employee base pay rate
private int empPieces;  //extra pieces made by employee
private int empType;  //employee type
private static int empCount = 0;  //number of objects in memory

//initialize employee, add 1 to employee count
public Employee( String first)
{
empName = first;
empCount++; //increment static count of employees
System.out.printf("Employee Name: %s %s; count = %d\n,",
empName, empCount);
}//end employee constructor

}//end of employee class

```

Is This A Good Question/Topic? 0

## Replies To: How to create random number ?

### #2 zim1985

• Grand Inquisitor

Reputation: 75
• Posts: 568
• Joined: 19-February 10

## Re: How to create random number ?

Posted 08 March 2010 - 11:01 PM

For a random number you could most easily just do this:
```int number = (int)(Math.random()*upperLimit + lowerLimit);

```

For instance...
```int number = (int)(Math.random()*10 + 1);

```

number will be from 1-10.
```int number = (int)(Math.random()*10 + 2);

```

number will be from 2-11.

That said, make sure that adding numbers at the end won't make the upperLimit of the random number too much.

This post has been edited by zim1985: 08 March 2010 - 11:16 PM

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12306
• Posts: 45,403
• Joined: 27-December 08

## Re: How to create random number ?

Posted 08 March 2010 - 11:07 PM

@zim1985: It looks like you have an extra closing parenthesis in your two examples without a corresponding opening parenthesis.

@biggmann: You might also want to check out the Random class if you don't want to use Math.random().

### #4 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: How to create random number ?

Posted 08 March 2010 - 11:11 PM

Have a look at Random Numbers in Java

Here's the API page: Class Random

You want nextInt(int n)

### #5 zim1985

• Grand Inquisitor

Reputation: 75
• Posts: 568
• Joined: 19-February 10

## Re: How to create random number ?

Posted 08 March 2010 - 11:17 PM

macosxnerd101, on 08 March 2010 - 09:07 PM, said:

@zim1985: It looks like you have an extra closing parenthesis in your two examples without a corresponding opening parenthesis.

I did rush a tad, thanks for pointing out my typo

### #6 biggmann

Reputation: 0
• Posts: 77
• Joined: 02-February 10

## Re: How to create random number ?

Posted 09 March 2010 - 08:06 AM

ok thank you for the help, I will try and get it going with the information you guys gave me.