Need help with generate random number

Writing out a code for random

Page 1 of 1

1 Replies - 849 Views - Last Post: 08 March 2009 - 09:39 AM Rate Topic: -----

#1 kill99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-May 08

Need help with generate random number

Posted 08 March 2009 - 09:30 AM

import java.text.*;

public class Employee
{
	String firstname;
	String lastname;
	int id;
	double salary;
	char grade;

	DecimalFormat df = new DecimalFormat("$0.00");

	public Employee(String fn,String ln, int id,double sal,char g)
	{
		firstname = fn;
		lastname = ln;
		id = id;
		salary = sal;
		grade = g;
	}
	public void deposit(double amt)
		{
			System.out.println("\nAdd more salary by : " + df.format(salary));
			if (amt > 0)
		   salary = salary + amt;
		   toString();
		}
	
	public void display()
	{
		System.out.println("First Name :" + firstname);
		System.out.println("Last Name :" + lastname);
		System.out.println("ID :" + id);
		System.out.println("Salary :" + salary);
		System.out.println("grade :" + grade);
	}

	//public Employee(String firstname, String lastname, int id, double salary, String grade)
	//{
		//this.firstname = firstname;
		//this.lastname = lastname;
		//this.id = id;
		//this.salary = salary;
		//this.grade = grade;
	//}

	public String toString()
	   {
			return "First Name	: " + firstname +
				   "Last Name	 : " + lastname +
				   "ID			: " + id +
				   "\nSalary	  : " + df.format(salary) +
				   "Grade		 : " + grade;

	   }

	public String getFirstname() { return firstname; }
	public String getLastname() { return lastname; }
	public int getId() { return id; }
	public double getSalary() { return salary;}
	public char getGrade() { return grade; }
}



The above code is able to work just that i do not knw hw to made a code to write a random number with a checker to ensure that the number will not be duplicate need some people to explain to me or help me out

public class UseEmployee
{
	public static void main (String args[]) throws Exception
	{
		Employee x = new Employee("Daniel", "Liew", 87, 1200, 'P');
		x.display();

		x.paycut(100);
		x.display();

		x.deposit(300);
		x.display();
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with generate random number

#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Need help with generate random number

Posted 08 March 2009 - 09:39 AM

Quote

The above code is able to work just that i do not know how to made a code to write a random number with a checker to ensure that the number will not be duplicate need some people to explain to me or help me out

One way to do this is to create an array that stores the numbers that are generated, once your program generates a number, you check if the array already contains that number, try binary search in that case. If the generated number appears to be already there then you dont write into the array and regenerate another number, if the number is not already contained in the array then the program will have to resize the array(increase it's capacity) and add the generated number with the other numbers in the array. I hope i was clear enough and i hope this helps, good luck :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1