# to generate random lottery no

• 07-07-2011, 07:00 AM
fakepics500
to generate random lottery no
i am new to java ,i wanted to make program to generate random lottery no using bigInteger
Code:

```import java.util.*; import java.Math.BigInteger; class lottery {         public static void main(String ar[])         {                 Random r= new Random();                 BigInteger b1=BigInteger.valueOf(100000000);                 BigInteger b2=BigInteger.valueOf(r.nextInt(100000000));                 BigInteger b=B1.add(B2);                 System.out.println("your lottery no="+b);         } }```
after help of everyone code modefied to
Code:

```import java.util.*; import java.math.BigInteger; class Lottery {         public static void main(String ar[])         {                 Random r= new Random();                 int ch=0;                 do                 {                     BigInteger b= new BigInteger(30,r);                 BigInteger b1= BigInteger.valueOf(100000000);                 if(b.compareTo(b1)>0)                 {                       System.out.println("your lottery no="+b);                         ch=0;                 }                 else                 {                         System.out.println("try again");                         ch=1;                 }                 }while(ch==1);         }```
• 07-07-2011, 07:16 AM
pbrockway2
Does your program compile? If it does not and you cannot understand the compiler messages, post them.

-----

Your class should really be called Lottery rather than lottery.

Do you mean all numbers to be equally likely? If so you cannot pick numbers at random and add them. This is illustrated by looking at all nine numbers that result from adding random numbers from 0->2:

0+0=0
0+1=1
0+2=2
1+0=1
1+1=2
1+2=3
2+0=2
2+1=3
2+2=4

2 will occur 33% of the time (3/9) but 4 will only occur 11% (1/9). The same applies for numbers drawn from a larger range: the numbers near the middle will be favoured.
• 07-07-2011, 07:45 AM
Uhm, only the second number was random. ;-)
• 07-07-2011, 07:46 AM
Quote:

Originally Posted by fakepics500
is there any other way to make this program

That is the wrong question. There is always "another way to do it". Describe what your problem is and we can help you with it.
• 07-07-2011, 07:50 AM
pbrockway2
Quote:

Uhm, only the second number was random. ;-)

Oops, you're right.
• 07-07-2011, 08:05 AM
fakepics500
Quote:

Originally Posted by pbrockway2
Does your program compile? If it does not and you cannot understand the compiler messages, post them.

yes my program complies and runs
-----

Code:

`Your class should really be called [b]L[/b]ottery rather than lottery.`
Code:

```Do you mean all numbers to be equally likely?  If so you cannot pick numbers at random and add them.  This is illustrated by looking at all nine numbers that result from adding random numbers from 0->2: 0+0=0 0+1=1 0+2=2 1+0=1 1+1=2 1+2=3 2+0=2 2+1=3 2+2=4 2 will occur 33% of the time (3/9) but 4 will only occur 11% (1/9).  The same applies for numbers drawn from a larger range: the numbers near the middle will be favoured.```
i want all no to be equal likely
• 07-07-2011, 08:07 AM
fakepics500
Quote:

Uhm, only the second number was random. ;-)

i want the lottery no should be greater than 100000000 therefor i used 2 BigIntegers

one to produce random no and one make the lottery no greater than 100000000
• 07-07-2011, 08:08 AM
DarrylBurke
Quote:

Code Conventions for the Java(TM) Programming Language: Contents

db
• 07-07-2011, 08:09 AM
fakepics500
Quote:

That is the wrong question. There is always "another way to do it". Describe what your problem is and we can help you with it.

i want the lottery no be greater than 100000000 therfore i am using two BigIntegers
i only want to use only one BigInteger
• 07-07-2011, 08:11 AM
sunde887
Java naming convention is to capitalize all first letters of each letter in a class
Code:

```public class MyClass public class AnotherClass ... etc```
And methods should have a lowercase first letter first word and then capitalized the first letter of all subsequent words.

Code:

```methodOne() methodTwo() methodThree() ... etc```
• 07-07-2011, 08:11 AM
fakepics500
Quote:

Originally Posted by DarrylBurke

thanks for helping :(clap):
• 07-07-2011, 08:12 AM
fakepics500
Quote:

Originally Posted by sunde887
Java naming convention is to capitalize all first letters of each letter in a class
Code:

```public class MyClass public class AnotherClass ... etc```
And methods should have a lowercase first letter first word and then capitalized the first letter of all subsequent words.

Code:

```methodOne() methodTwo() methodThree() ... etc```

thanks:(clap):
• 07-07-2011, 08:14 AM
sunde887
Perhaps you can generate a random number with x and then concatenate it with a string and pass it into the constrctor of big integer

Code:

```int x = ...; int y = rand.nextInt(); String s = (x + y) + "";```
I'll leave the rest for you to figure out.
• 07-07-2011, 08:20 AM
Quote:

Originally Posted by fakepics500
yes my program complies and runs

Then what, exactly, is your problem?

Quote:

Originally Posted by fakepics500
i want the lottery no should be greater than 100000000 therefor i used 2 BigIntegers

one to produce random no and one make the lottery no greater than 100000000

I realised that and simply pointed out to pbrockway that the stuff about the additions and random numbers and frequency was not needed (it was also, however, not wasted).
• 07-07-2011, 08:44 AM
fakepics500
Quote:

Originally Posted by sunde887
Perhaps you can generate a random number with x and then concatenate it with a string and pass it into the constrctor of big integer

Code:

```int x = ...; int y = rand.nextInt(); String s = (x + y) + "";```
I'll leave the rest for you to figure out.

why are you genrating tw0 random no and then adding them
i modefied code little bit
Code:

```import java.util.*; import java.math.BigInteger; class Lottery {         public static void main(String ar[])         {                 Random r= new Random();                     BigInteger b= new BigInteger(100,r);                 System.out.println("your lottery no="+b);         } }```
but there is one problem how can check genereted lottery no is greater than 10000000
can i use
Code:

```if(b>1000000) { System.out.println("b="+b); }```
but this will make lottery ticket no to non equal likely
:(sweat):
• 07-08-2011, 07:47 AM
pbrockway2
Quote:

but there is one problem how can check genereted lottery no is greater than 10000000
You can use the BigInteger method compareTo().

-----

Code:

`BigInteger b= new BigInteger(100,r);`
Do you really mean this? How big is 10000000 (say in terms of Bill Gate's income)? How big are the numbers generated by BigInteger(100,r)?

-----

In Java int (as distinct from BigInteger) goes up to 2^31. That's 2147483648 which is somewhat larger than 100000000. So there's really no need to drag BigInteger into the problem.
• 07-08-2011, 08:26 AM
fakepics500
Quote:

Originally Posted by pbrockway2
You can use the BigInteger method compareTo()

thanks for reminding me of compareTo

-----
Quote:

Code:

`BigInteger b= new BigInteger(100,r);`
Do you really mean this? How big is 10000000 (say in terms of Bill Gate's income)? How big are the numbers generated by BigInteger(100,r)?

-----

In Java int (as distinct from BigInteger) goes up to 2^31. That's 2147483648 which is somewhat larger than 100000000. So there's really no need to drag BigInteger into the problem.
thanks for correcting me

Code:

```import java.util.*; import java.math.BigInteger; class Lottery {         public static void main(String ar[])         {                 Random r= new Random();                 BigInteger b= new BigInteger(30,r);                 BigInteger b1= BigInteger.valueOf(100000000);                 if(b.compareTo(b1)>0)                 {                       System.out.println("your lottery no="+b);                 }                 else                         System.out.println("try again");         } }```
i want to use a jump statement in my else
• 07-08-2011, 08:29 AM
sunde887
Generally you should use a loop rather than using a jump statement. Do while, or while will both work fine.
• 07-08-2011, 08:36 AM
fakepics500
Quote:

Originally Posted by sunde887
Generally you should use a loop rather than using a jump statement. Do while, or while will both work fine.

Code:

```import java.util.*; import java.math.BigInteger; class Lottery {         public static void main(String ar[])         {                 Random r= new Random();                 int ch=0;                 do                 {                     BigInteger b= new BigInteger(30,r);                 BigInteger b1= BigInteger.valueOf(100000000);                 if(b.compareTo(b1)>0)                 {                       System.out.println("your lottery no="+b);                         ch=0;                 }                 else                 {                                         ch=1;                 }                 }while(ch==1);         }```
at last its over thanks everybodys help
• 07-08-2011, 09:14 AM
sunde887
While that works it's probably better to use the compareTo in a condition rather than using a flag.

Code:

```do{   create big integer } while(compare);```
Basically keep looping as long as the random number is not greater than the threshold(1000000)