Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Randomly picking variables

  1. #1
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Smile Randomly picking variables

    Hello.

    I have four variables, p1, p2, p3 and p4. How do I use the Random util to make it so that one of them is randomly selected?

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Randomly picking variables

    Arrays would be the better option: make an array of size 4, and pick a random int from 0 to 3 as the index.

    Failing that, your only option is a switch statement or if-else chain.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Randomly picking variables

    Another option is to place all your values into a List and use Collections.shuffle.

  4. #4
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Default Re: Randomly picking variables

    Thank you, I will be using an if-else chain.

    But how do I then bind the ints with the variables?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Randomly picking variables

    Huh?

    How about you tell us what you are trying to achieve instead of the solution you have arrived at.

  6. #6
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Default Re: Randomly picking variables

    I am trying to make the computer randomly select one of my specified variables.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Randomly picking variables

    How are you using the variables? Are you getting values from them or putting values into them?
    Another way to select 1 of 4 statements to execute would be a switch statement.

  8. #8
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Default Re: Randomly picking variables

    The values at this point are not anything of interest, because I am using
    pass1.requestFocus(); }
    To focus on pass1, after a value has been entered it will randomize the next pass, but excluding the currently picked one and all others (pass1 is a text field, one of 4)

    This will be a very dirty code and I do not mind having 100 if-statements in a row just to finish it
    Last edited by AlawiGMX; 11-30-2011 at 12:19 AM.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Randomly picking variables

    Quote Originally Posted by AlawiGMX View Post
    I am trying to make the computer randomly select one of my specified variables.
    No, that is your perceived solution. Provide details of the overall aim of the program. Such as you are writing an Address book application.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Randomly picking variables

    You have 4 different sources that you want to randomly chose one of and get its value into a varible.
    Generate a random number from 0-3 and use that in a switch or chained if/else if with each case or true statement in the if setting that variable from one of the 4 sources.

    If the sources are similar objects, put them into an array and index the array with a random value.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Randomly picking variables

    Is this a follow up to the password checking program?

  12. #12
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Default Re: Randomly picking variables

    The values at this point are not anything of interest, because I am using
    pass1.requestFocus(); }
    To focus on pass1, after a value has been entered it will randomize the next pass, but excluding the currently picked one and all others (pass1 is a text field, one of 4)

    This will be a very dirty code and I do not mind having 100 if-statements in a row just to finish it

    Junky I would like for you to stop posting here as you are not helpful at all. Please let me get real help.

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

    Default Re: Randomly picking variables

    I am trying to make the computer randomly select one of my specified variables.
    In a later post you say "pass1 is a text field". It is important to realise that variables like pass1 have values (references to things like text fields). At runtime what is randomly selected is a value - a value that can be assigned to any variable whatsoever for all that it matters to the program.

    Chains of if/else, or a switch statement seem odd given the more straightforward suggestions of randomly selecting an index into an array or shuffling a list.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Randomly picking variables

    Quote Originally Posted by AlawiGMX View Post
    Junky I would like for you to stop posting here as you are not helpful at all. Please let me get real help.
    Well you have no control over who does and doesn't post. But rest assured you will never get any help from me ever again. I am just stunned at how obnoxious and ungrateful some idiots are when asking for help!

  15. #15
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Default Re: Randomly picking variables

    Thank you, I will be randomly selecting an int from 0-3 and then use a switch statement, I will post the results then I am done, thanks a million times.

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

    Default Re: Randomly picking variables

    Junky I would like for you ...
    To put it bluntly, so what?

    -----

    Junky's post serves the rest of us as a "red flag" suggesting we might benefit from reading your previous posts. No-body wants to waste their time where there might be a history, and an attitude.

    Bye.
    Fubarable likes this.

Similar Threads

  1. Replies: 8
    Last Post: 11-22-2010, 11:09 AM
  2. Picking randomly
    By AbdulAziz Bader in forum New To Java
    Replies: 17
    Last Post: 06-02-2010, 01:39 PM
  3. 3D Duke picking his nose and contemplating on Ruby
    By Mark in forum Reviews / Advertising
    Replies: 2
    Last Post: 12-28-2007, 04:00 PM
  4. Randomly accessing files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-12-2007, 11:46 AM
  5. Accessing a file randomly
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 09:15 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
  •