This week's giveaway is in the Java/Jakarta EE forum.We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Choosing 'random' numbers from set...

Ranch Hand
Posts: 102
How do I choose a number from random in a set? Say I want to randomly select '2, 4, 6, 8, 10' or '3, 6, 9, 12, 15, 18'...etc. I'm thinking somehow I'll be using 'mod', but I can't make it work. Any ideas?

Ranch Hand
Posts: 1050
2
Java has a built in random method. Don't remember where it is at the moment. (No need to try and write one yourself.)

lowercase baba
Bartender
Posts: 12602
50
you can use the random function to generate an index. then, have a method that calculates the value in the set.
i.e. you want an even number between 2 and 20, inclusive. generate a random number between 1 and 10, then multiply by 2.

David Crossett
Ranch Hand
Posts: 102
Thanks for the 'even' idea - that worked! This is an assignment, and apparently there is supposed to be a way to get these random number using only one line of code. So I got the evens now - how about the other ones? How do I get a random number out of the set 3, 5, 7, 9, 11? Obviously there is a pattern here. Also, I gotta figure out how to get number out of set 6, 10, 14, 18, 22. So there must be a simple way to change the formula of:
y = a + ( Math.random() * b );
where 'a' is the starting number and 'b' is the 'magnitude' or 'width' of the random numbers. I'm sure there are variations to this formula to solve my problem (for example, "1/2b then * 2" gave me even numbers in a range.) Thanks for any help!

fred rosenberger
lowercase baba
Bartender
Posts: 12602
50
personally, i hate assignements like this. they're less about learning how to program than figuring out some math "trick". And i'm saying this as a former high school math teacher, and current java programmer.
what you have here are basically linear progressions.
3,5,7,9... is the same formula as the even numbers, except after you multiply by 2, add 1.
3,6,9,12... you mult by 3.
there are three "variables" in your formula. on is the number of numbers in your set. every example you've listed has five (2,4,6,8,10) or (6,10,14,18,22). so, you need to get a random number from 0-4 (or 1-5, or 7-11, it doesn't matter - whatever works for you). probably the easiest (for the rest of the computation) would be to get from 1-5.
then, you need look at how much the numbers "jump" each time. in your last example, of [6,10,14,18,22] you are always increasing by 4. so, you will need to mulitply your random number by this.
i.e. i could get 1-5, so i multiply by 4, giving me 4,8,12,16 or 20. this gets us pretty close, but each one is 2 too small. so... add 2.
this only works if the numbers are always increasing by the same amout.
so your one line would be something like
(((1 + (int)(Math.random() * (5))) * 4) + 2)
// Math.random() - value between 0 (inc) and 1 (exc)
// * 5 - now value between 0(inc) and 5(exc)
// (int) - now have value 0,1,2,3 or 4
// + 1 - now have value 1,2,3,4,5
// * 4 - 4,8,12,16,20
// + 2 - 6,10,14,18,22
NOTE: this is extremely bad code. it's hard to read, it's ugly, complicated, there are 8 sets of parentisis (sp?)...
if i saw code like this, i'd break it up into several lines, just to figure out what the heck it was doing, or i'd (at the very least) comment the hell out of it.
just because you CAN do something in one line, ask yourself if you should.
of course, there might even be an easier way, but that's how this former math teacher sees it.

 It is sorta covered in the JavaRanch Style Guide.