Results 1 to 20 of 20
Thread: to generate random lottery no
 07072011, 08:00 AM #1Member
 Join Date
 Jul 2011
 Posts
 43
 Rep Power
 0
to generate random lottery no
i am new to java ,i wanted to make program to generate random lottery no using bigInteger
Java 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); } }
Java 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); }
Last edited by fakepics500; 07082011 at 09:38 AM.
 07072011, 08:16 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 17
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.
 07072011, 08:45 AM #3Senior Member
 Join Date
 Jun 2008
 Posts
 2,568
 Rep Power
 14
Uhm, only the second number was random. ;)
 07072011, 08:46 AM #4Senior Member
 Join Date
 Jun 2008
 Posts
 2,568
 Rep Power
 14
 07072011, 08:50 AM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 17
 07072011, 09:05 AM #6Member
 Join Date
 Jul 2011
 Posts
 43
 Rep Power
 0
yes my program complies and runs

Java Code:Your class should really be called [b]L[/b]ottery rather than lottery.
Java 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.
 07072011, 09:07 AM #7Member
 Join Date
 Jul 2011
 Posts
 43
 Rep Power
 0
 07072011, 09:08 AM #8can you explain why it should start with L then l
db
 07072011, 09:09 AM #9Member
 Join Date
 Jul 2011
 Posts
 43
 Rep Power
 0
 07072011, 09:11 AM #10
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,068
 Blog Entries
 3
 Rep Power
 13
Java naming convention is to capitalize all first letters of each letter in a class
Java Code:public class MyClass public class AnotherClass ... etc
Java Code:methodOne() methodTwo() methodThree() ... etc
 07072011, 09:11 AM #11Member
 Join Date
 Jul 2011
 Posts
 43
 Rep Power
 0
 07072011, 09:12 AM #12Member
 Join Date
 Jul 2011
 Posts
 43
 Rep Power
 0
 07072011, 09:14 AM #13
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,068
 Blog Entries
 3
 Rep Power
 13
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
Java Code:int x = ...; int y = rand.nextInt(); String s = (x + y) + "";
 07072011, 09:20 AM #14Senior Member
 Join Date
 Jun 2008
 Posts
 2,568
 Rep Power
 14
 07072011, 09:44 AM #15Member
 Join Date
 Jul 2011
 Posts
 43
 Rep Power
 0
why are you genrating tw0 random no and then adding them
i modefied code little bit
Java 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); } }
can i use
Java Code:if(b>1000000) { System.out.println("b="+b); }
 07082011, 08:47 AM #16Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 17
but there is one problem how can check genereted lottery no is greater than 10000000

Java Code:BigInteger b= new 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.
 07082011, 09:26 AM #17Member
 Join Date
 Jul 2011
 Posts
 43
 Rep Power
 0
thanks for reminding me of compareTo

Java Code:BigInteger b= new 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.
Java 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"); } }
 07082011, 09:29 AM #18
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,068
 Blog Entries
 3
 Rep Power
 13
Generally you should use a loop rather than using a jump statement. Do while, or while will both work fine.
 07082011, 09:36 AM #19Member
 Join Date
 Jul 2011
 Posts
 43
 Rep Power
 0
Java 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); }
Last edited by fakepics500; 07082011 at 09:39 AM.
 07082011, 10:14 AM #20
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,068
 Blog Entries
 3
 Rep Power
 13
While that works it's probably better to use the compareTo in a condition rather than using a flag.
Java Code:do{ create big integer } while(compare);
Similar Threads

Random Lottery Numbers
By ComicStix in forum New To JavaReplies: 2Last Post: 04292011, 07:18 AM 
Random integer generate
By trbLeeciN in forum New To JavaReplies: 6Last Post: 06222010, 02:19 AM 
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04192009, 06:50 PM 
Trying to Generate Random number
By PeterFeng in forum New To JavaReplies: 10Last Post: 01142009, 09:37 AM 
Generate a random number
By romina in forum New To JavaReplies: 1Last Post: 08072007, 06:23 AM
Bookmarks