Member Avatar for mickdrealist

I need to write a Java program that uses a Monte Carlo algorithm to calculate the
probability that next week’s lottery draw won’t have any consecutive
pairs of numbers (e.g. 8 and 9 or 22 and 23). 6 numbers are drawn
from 1 to 45.

Recommended Answers

All 12 Replies

And your point is? If you need help with anything, post what you have so far.
Just to be clear: when I say "what you have so far", in case you missunderstood it, I didn't mean "post your assignment and we'll do it for you."

The rules of Daniweb demand that you at the very least show that you've done some effort to write the code yourself.

stultuske may not be the most diplomatic person here, but his message is right...
DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Member Avatar for mickdrealist

I do apologise guys. Forgot that tiny detail. I'll post what I have shortly. Thanks for your replies. Will keep in mind next time.

Member Avatar for mickdrealist

here is my code. I'm not sure i got the non repeating numbers right.

package lab1;

import java.util.Random;

public class Lottery {

    public static void main(String[] args) {

        int [] x = new int[7];

        for(int i = 1; i<x.length; i++){
            int number = (int) (Math.random() * 45 + 1);
            System.out.print(number + " ");

            if(i == i {
                break;
            }
            if(i == i+1){
                break;
            }
        }

    }
}
Member Avatar for mickdrealist

thanks for your help

The below code snippet is not correct. One of the 'i''s should be replaced with 'number'

            if(i == i {
                break;
            }
            if(i == i+1){
                break;
            }

Another suggestion: please use functions. This would make the program look better
Eg:

isNumberInSequence(number);


boolean isNumberInSequence(int number){
// your code here
}

You said that you need to calculate, the probability, but I do not see any logic for this. Your logic, just generates a random number and prints it. Try to put more thought into it.
Hint: You will need a bunch of random numbers since its a lottery. Not numbers from 1 to 7.

newcoder: "please use functions" ..
since Java doesn't have functions, that might pose bit of a problem.
mickdrealist: you never store the 'random' numbers you generate, so how would you be able to compare previous values to the new one(s) ?

stultuske: you are correct. "Methods" is the appropriate term.

Member Avatar for iamthwee

You will need a bunch of random numbers since its a lottery. Not numbers from 1 to 7.

(S)he's not generating number 1-7 this is merely where he is going to store the seven numbers picked.

As I said before shuffling the 1-49 in an array and picking the first seven is a better way to generate the numbers than the method supplied.

For such a small dataset the results won't be noticeable so he can probably get away with it here, for much larger datasets it will matter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.