27th March 2012 In class mini project samples "Lottery Programme"

Go down

27th March 2012 In class mini project samples "Lottery Programme"

Post  Alex-JustIT on Fri Mar 30, 2012 7:53 pm

Hi Guys,

It was glad to get to meet new DPP candidates and still seeing familiar faces. I believed that the session went well and we have more communications between each other and most important we have some insights into what we want more. Gareth is doing very well advising others such as Raheem. Raheem finally dig down into codes. WELL DONE!!!

Some of you have also suggested a group project which I think is an excellent idea. What I have to do is I will do some research and see what I could find but I have to start doing that from the 10th of April as I will be away next week. (If you guys don't mind)

Below are the sample codes about the method used for generating Random numbers and comparing user lottery guess input with the 5 random numbers:


public Lottery() {
Random rand = new Random(System.currentTimeMillis());
lotteryNumbers = new int[5];
for (int i = 0; i < lotteryNumbers.length; i++) {
lotteryNumbers[i] = Math.abs(rand.nextInt()) % 10;
}
}



public int compareNumbers(int[] usersNumbers) {
int match = 0;
if (usersNumbers.length == lotteryNumbers.length) {
for (int i = 0; i < lotteryNumbers.length; i++) {
if (usersNumbers[i] == lotteryNumbers[i]) {
match++;
}
}
}
return match;
}


Regards,
Alex

Alex-JustIT
Trainer
Trainer

Posts : 10
Join date : 2011-07-19

View user profile

Back to top Go down

Re: 27th March 2012 In class mini project samples "Lottery Programme"

Post  Arunas on Fri Mar 30, 2012 11:35 pm

Hi,
I created a test class to test your solution. I think there is a mistake. Your random numbers produced aren’t unique. Go through my test class bellow and see if I didn’t miss anything.

System.currentTimeMillis() method will give you a seed for rand object based on your system time. However, you are using same rand object in your loop which will produce random, but not unique numbers (correct me if I am wrong).

The only solution I can think is to “filter” same random numbers. Easiest way I can think is using Set which doesn’t allow same numbers to be stored within. If anyone is interested I can post the solution (it's really simple) for you guys to study.

Cheers
Arunas

Results produced:
[9, 1, 1, 0, 1] - not unique
[7, 1, 1, 6, 7] - not unique
[0, 0, 0, 6, 1] - not unique
[7, 2, 4, 0, 1] - etc.
[6, 5, 8, 5, 4]
[5, 4, 3, 7, 3]
[2, 2, 4, 2, 1]
[6, 8, 6, 8, 2]
[3, 0, 6, 1, 4]
[1, 5, 4, 7, 0]

public class Test {

// testing
static ArrayList Lottery()
{
Random rand = new Random(System.currentTimeMillis());
// I am using Array List to make the code shorter.
// Array List returns nice string representation of the object
// (no need to iterate array to print results)
ArrayList list = new ArrayList();

for (int i = 0; i < 5; i++)
{
// add 5 random numbers to Array List
list.add(Math.abs(rand.nextInt()) % 10);
}
return list; // return Array List with 5 numbers
}

public static void main (String [] args)
{
// run Lottery method 10 times to observe results
for(int counter = 0; counter < 10; counter++)
{
try
{
// make the current thread stop for 10 milliseconds. This is
// required because for loop iterates so fast that
// System.currentTimeMillis() method will take the same time
// from the system and results will be the same (random object
// will have the same seed each iteration)
Thread.sleep(10);
}
catch (InterruptedException ex) // catch exception
{
System.err.print("Error occured\n" + ex);
}
// observe results
System.out.println(Lottery());
} // end for loop

} // end main
} // end class

Arunas
Newbie
Newbie

Posts : 5
Join date : 2012-03-28

View user profile

Back to top Go down

Re: 27th March 2012 In class mini project samples "Lottery Programme"

Post  Alex-JustIT on Wed Apr 18, 2012 3:44 am

Hi Arunus,

Sorry about such as late reply.

I also believe you are right. I forgot to think about that and I think what I was trying to achieve is just to get new random number that at least some of the values between 5 numbers are not the same. but yeah you are absolutely right.

What method did you used to create unique number? I am interested.

Alex-JustIT
Trainer
Trainer

Posts : 10
Join date : 2011-07-19

View user profile

Back to top Go down

Re: 27th March 2012 In class mini project samples "Lottery Programme"

Post  Arunas on Fri Apr 20, 2012 6:42 am

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import javax.swing.JOptionPane;

/**
*
* @author arunas
*/
public class RandomNums {

public static void main(String [] args)
{
compareNumbers(generateNumbers());
}

static ArrayList<Integer> generateNumbers()
{
Random number = new Random();
Set<Integer> set = new HashSet<>(); // filters duplicates automatically

do
{
set.add(number.nextInt(10));
}
while(set.size() < 5);

return new ArrayList<>(set);
}

static void compareNumbers(ArrayList<Integer> list)
{
// can use arrays but ArrayList prints nice String representation (no for loops and etc.)
ArrayList <Integer> userNums = new ArrayList<>(); // store user numbers
ArrayList <Integer> match = new ArrayList<>(); // store numbers that match

// very simle GUI for taking numbers and processing data (no exception handling)
for(int count = 0; count < list.size(); count++)
{
userNums.add(Integer.parseInt(JOptionPane.showInputDialog("Enter number: " + (count + 1))));

if(list.contains(userNums.get(count)))
match.add(userNums.get(count));
}

System.out.println("Generated random numbers are: " + list);
System.out.println("User numbers are: " + userNums);
System.out.println("Numbers that match: " + match);
}
}


Arunas


Arunas
Newbie
Newbie

Posts : 5
Join date : 2012-03-28

View user profile

Back to top Go down

Re: 27th March 2012 In class mini project samples "Lottery Programme"

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum