• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Choosing 'random' numbers from set...

 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has a built in random method. Don't remember where it is at the moment. (No need to try and write one yourself.)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic