Results 1 to 4 of 4
  1. #1
    rldme is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Java, Random and ?

    Can you tell me what the "?" mark is in this code (i.e. what its called)

    Random rand = new Random();
    int choice = (rand.nextInt(100)<75) ? 1:0;

    When the code is run it returns either 1 or 0.
    It returns 1 75% of the time.
    I have played with the 1:0 values if you change it to 5:10 for example it returns the number 5, 75% of the time.
    I want to understand what the ? is doing. I get that it some how identifies that of the two values it returns the first one 75% more than the second one.
    And if you change the 75 to 25 it would then return the first value (1) 25% of the time vs. second value.
    But I can't find any documentation on the ? or what other options I could use in place of it.

    Thanks

  2. #2
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Java, Random and ?

    The question mark is just a different form of the if statement. The following two snippets of code do exactly the same things:
    Java Code:
    public Foo generateFoo(int threshold)
    {
        if(threshold > MIN_THRESHOLD)
           return new Foo();
        else
           return null;
    }
    Java Code:
    public Foo generateFoo(in threshold)
    {
        return (threshold > MIN_THRESHOLD) ? new Foo() : null;
    }
    Last edited by Fubarable; 10-07-2012 at 11:25 PM. Reason: Foo return type added

  3. #3
    rldme is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: Java, Random and ?

    Thank you now I remember that aspect. Have a great day.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Java, Random and ?

    It's called a ternary expression.

    Quote Originally Posted by penguinCoder View Post
    The question mark is just a different form of the if statement.
    Not really, no. The ternary expression is a value expression which must evaluate to a valid value.

    This won't compile, because void isn't a valid value:
    Java Code:
    ... {
      boolean condn = false; // or true
      condn ? setInt(0) : setInt(1);
    }
    
    void setInt(int value) {
      ...
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 01-23-2012, 09:12 AM
  2. Java random number
    By Mezgrath in forum New To Java
    Replies: 3
    Last Post: 11-22-2009, 06:15 AM
  3. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 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
  •