Results 1 to 16 of 16
Thread: Randomly picking variables
- 11-29-2011, 10:59 PM #1
- 11-29-2011, 11:05 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
- 11-29-2011, 11:07 PM #3
Re: Randomly picking variables
Another option is to place all your values into a List and use Collections.shuffle.
- 11-29-2011, 11:08 PM #4
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?
- 11-29-2011, 11:10 PM #5
Re: Randomly picking variables
Huh?
How about you tell us what you are trying to achieve instead of the solution you have arrived at.
- 11-29-2011, 11:12 PM #6
Re: Randomly picking variables
I am trying to make the computer randomly select one of my specified variables.
- 11-29-2011, 11:13 PM #7
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.
- 11-29-2011, 11:15 PM #8
Re: Randomly picking variables
The values at this point are not anything of interest, because I am using
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)pass1.requestFocus(); }
This will be a very dirty code and I do not mind having 100 if-statements in a row just to finish itLast edited by AlawiGMX; 11-29-2011 at 11:19 PM.
- 11-29-2011, 11:18 PM #9
- 11-29-2011, 11:18 PM #10
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-29-2011, 11:19 PM #11
Re: Randomly picking variables
Is this a follow up to the password checking program?
- 11-29-2011, 11:22 PM #12
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.
- 11-29-2011, 11:28 PM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Randomly picking 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.I am trying to make the computer randomly select one of my specified variables.
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.
- 11-29-2011, 11:31 PM #14
- 11-29-2011, 11:34 PM #15
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.
- 11-29-2011, 11:36 PM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Picking a Collection and Design for querying 1G Point objects
By ennuages in forum Advanced JavaReplies: 8Last Post: 11-22-2010, 10:09 AM -
Picking randomly
By AbdulAziz Bader in forum New To JavaReplies: 17Last Post: 06-02-2010, 12:39 PM -
3D Duke picking his nose and contemplating on Ruby
By Mark in forum Reviews / AdvertisingReplies: 2Last Post: 12-28-2007, 03:00 PM -
Randomly accessing files
By Java Tip in forum Java TipReplies: 0Last Post: 12-12-2007, 10:46 AM -
Accessing a file randomly
By Java Tip in forum Java TipReplies: 0Last Post: 11-10-2007, 08:15 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks