Results 1 to 20 of 20
Thread: to generate random lottery no
- 07-07-2011, 07:00 AM #1
Member
- 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
after help of everyone code modefied toJava 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; 07-08-2011 at 08:38 AM.
- 07-07-2011, 07:16 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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 #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Uhm, only the second number was random. ;-)
- 07-07-2011, 07:46 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 07-07-2011, 07:50 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
- 07-07-2011, 08:05 AM #6
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
yes my program complies and runs
-----
can you explain why it should start with L then lJava Code:Your class should really be called [b]L[/b]ottery rather than lottery.
i want all no to be equal likelyJava 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.
- 07-07-2011, 08:07 AM #7
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
- 07-07-2011, 08:08 AM #8
Code Conventions for the Java(TM) Programming Language: Contentscan you explain why it should start with L then l
db
- 07-07-2011, 08:09 AM #9
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
- 07-07-2011, 08:11 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Java naming convention is to capitalize all first letters of each letter in a class
And methods should have a lowercase first letter first word and then capitalized the first letter of all subsequent words.Java Code:public class MyClass public class AnotherClass ... etc
Java Code:methodOne() methodTwo() methodThree() ... etc
- 07-07-2011, 08:11 AM #11
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
- 07-07-2011, 08:12 AM #12
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
- 07-07-2011, 08:14 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
I'll leave the rest for you to figure out.Java Code:int x = ...; int y = rand.nextInt(); String s = (x + y) + "";
- 07-07-2011, 08:20 AM #14
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 07-07-2011, 08:44 AM #15
Member
- 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
but there is one problem how can check genereted lottery no is greater than 10000000Java 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
but this will make lottery ticket no to non equal likelyJava Code:if(b>1000000) { System.out.println("b="+b); }
- 07-08-2011, 07:47 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
You can use the BigInteger method compareTo().but there is one problem how can check genereted lottery no is greater than 10000000
-----
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)?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.
- 07-08-2011, 08:26 AM #17
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
thanks for reminding me of compareTo
-----
thanks for correcting meDo 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)?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.
i want to use a jump statement in my elseJava 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"); } }
- 07-08-2011, 08:29 AM #18
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 #19
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
at last its over thanks everybodys helpJava 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; 07-08-2011 at 08:39 AM.
- 07-08-2011, 09:14 AM #20
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
While that works it's probably better to use the compareTo in a condition rather than using a flag.
Basically keep looping as long as the random number is not greater than the threshold(1000000)Java Code:do{ create big integer } while(compare);
Similar Threads
-
Random Lottery Numbers
By ComicStix in forum New To JavaReplies: 2Last Post: 04-29-2011, 06:18 AM -
Random integer generate
By trbLeeciN in forum New To JavaReplies: 6Last Post: 06-22-2010, 01: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: 04-19-2009, 05:50 PM -
Trying to Generate Random number
By PeterFeng in forum New To JavaReplies: 10Last Post: 01-14-2009, 08:37 AM -
Generate a random number
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks