Results 1 to 20 of 20
  1. #1
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default 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);
             }
    }
    after help of everyone code modefied to
    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 09:38 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

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

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by fakepics500 View Post
    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.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by masijade View Post
    Uhm, only the second number was random. ;-)
    Oops, you're right.

  6. #6
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Does your program compile? If it does not and you cannot understand the compiler messages, post them.
    yes my program complies and runs
    -----

    Java Code:
    Your class should really be called [b]L[/b]ottery rather than lottery.
    can you explain why it should start with L then l
    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.
    i want all no to be equal likely

  7. #7
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    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

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    can you explain why it should start with L then l
    Code Conventions for the Java(TM) Programming Language: Contents

    db

  9. #9
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    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

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java naming convention is to capitalize all first letters of each letter in a class
    Java 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.

    Java Code:
    methodOne()
    methodTwo()
    methodThree()
    ...
    etc

  11. #11
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    thanks for helping

  12. #12
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Java naming convention is to capitalize all first letters of each letter in a class
    Java 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.

    Java Code:
    methodOne()
    methodTwo()
    methodThree()
    ...
    etc
    thanks

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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) + "";
    I'll leave the rest for you to figure out.

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by fakepics500 View Post
    yes my program complies and runs
    Then what, exactly, is your problem?

    Quote Originally Posted by fakepics500 View Post
    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).

  15. #15
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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) + "";
    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
    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);
             }
    }
    but there is one problem how can check genereted lottery no is greater than 10000000
    can i use
    Java Code:
    if(b>1000000)
    {
    System.out.println("b="+b);
    }
    but this will make lottery ticket no to non equal likely

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

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

    -----

    Java 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.

  17. #17
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    You can use the BigInteger method compareTo()
    thanks for reminding me of compareTo

    -----
    Java 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

    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");
             }
    }
    i want to use a jump statement in my else

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

  19. #19
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Generally you should use a loop rather than using a jump statement. Do while, or while will both work fine.
    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);
             }
    at last its over thanks everybodys help
    Last edited by fakepics500; 07-08-2011 at 09:39 AM.

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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);
    Basically keep looping as long as the random number is not greater than the threshold(1000000)

Similar Threads

  1. Random Lottery Numbers
    By ComicStix in forum New To Java
    Replies: 2
    Last Post: 04-29-2011, 07:18 AM
  2. Random integer generate
    By trbLeeciN in forum New To Java
    Replies: 6
    Last Post: 06-22-2010, 02:19 AM
  3. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  4. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 09:37 AM
  5. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •