Results 1 to 9 of 9
  1. #1
    eminbestek is offline Member
    Join Date
    Nov 2010
    Location
    Turkey
    Posts
    6
    Rep Power
    0

    Default Java Math.random help:S

    int generate = 1000 + (int)(Math.random()*9000);

    we can produce 4 digit number with this code between 1000 to 10000; but I want to make every digit different from each other. (e.g in 1959, 9 is used twice)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    You could generate 4 different single digits from 0-9, check to make sure they're unique, and combine them.

  3. #3
    eminbestek is offline Member
    Join Date
    Nov 2010
    Location
    Turkey
    Posts
    6
    Rep Power
    0

    Default

    okey, it's true. Then,For making different digit, I should control every digit in while and then generate the other digit, is it true?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    What happened when you tried that?

    You could also store the numbers in an array (or a List, or a Set) to check against when generating the next number.

    Or you could do it the brute force way: if you generate enough random numbers, eventually you'll end up with one that has all unique digits. Not pretty, but it would work.

  5. #5
    eminbestek is offline Member
    Join Date
    Nov 2010
    Location
    Turkey
    Posts
    6
    Rep Power
    0

    Default

    I control them in "while", It is running (not generating same digits)and I have to store them to use in the end of the program. I think it can be with array, but I don't know using it:S

  6. #6
    eminbestek is offline Member
    Join Date
    Nov 2010
    Location
    Turkey
    Posts
    6
    Rep Power
    0

    Default

    Thanks for your helps KevinWorkman :)

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    No problem. Does that mean you solved it? You might consider posting your solution so people with the same problem in the future will be able to learn from it.

  8. #8
    eminbestek is offline Member
    Join Date
    Nov 2010
    Location
    Turkey
    Posts
    6
    Rep Power
    0

    Default

    After I wrote the code, I will post it :)

  9. #9
    eminbestek is offline Member
    Join Date
    Nov 2010
    Location
    Turkey
    Posts
    6
    Rep Power
    0

    Default

    public class GenerateDifferentFourDigitNumber{
    public static void main(String[]args){
    int a, b, c, d;
    a = (int)(Math.random()*9+1); // a is between 1-9 inclusive
    do{
    b = (int)(Math.random()*10);
    }while(a==b); // b is between 0-9, inclusive, and different than a

    do{
    c = (int)(Math.random()*10);
    }while(c==b||c==a); // c is between 0-9, inclusive, and different than (a and b)

    do{
    d = (int)(Math.random()*10);
    }while(d==a||d==b||d==c||d==0); // d is between 0-9, inclusive, and different than (a and b and c)

    int result = d*1000+c*100+b*10+a; // compute the number.

    System.out.println(result); // print the number to screen
    }
    }

Similar Threads

  1. hi guys help me in math.random () in java
    By funkygarzon in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 05:48 AM
  2. Math.random not zero
    By rsvr in forum New To Java
    Replies: 3
    Last Post: 02-28-2010, 02:57 AM
  3. Math.random
    By p0rnstar in forum New To Java
    Replies: 9
    Last Post: 01-27-2010, 02:26 AM
  4. Math.random()
    By Dieter in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 10:28 AM
  5. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:09 PM

Posting Permissions

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