0
computerTotal = (int) Math.ceil(Math.random() * 21);

Can someone show me how to get 16 - 21 random number I keep getting errors when i try to implement the Math.floor function... As you can see i'm not very good at putting functions within functions.

Many Thanks!

erp
  • 2,950
  • 9
  • 45
  • 90
user2233480
  • 43
  • 1
  • 3
  • 9
  • `computerTotal = 16 + Math.round(Math.random()*5);` should give you a value between 16-21; – Shark Apr 08 '13 at 14:31
  • In short, you can use the Random class which generates random integers, or you can use Math.Random and scale the answer then add your floor value. The second approach slightly skews the randomness, so it is probably better to use the Random class - this is what it was made for! There's a full discussion of this here: http://stackoverflow.com/questions/363681/generating-random-number-in-a-range-with-java – Will Apr 08 '13 at 14:35
  • Thanks Will, should have been a little more thorough in my research. Sharks solution worked fine! Thanks gents. – user2233480 Apr 08 '13 at 14:44
  • You did research? lol maybe you should read [[www.whathaveyoutried.com]] – Shark Apr 08 '13 at 14:46
  • the word 'research' used very loosely. – user2233480 Apr 08 '13 at 14:50

2 Answers2

5

If Java, use the Random Class.

Random r = new Random();
int myRand = 16+ r.nextInt(6); //16+[0-6) = 16-21
Shark
  • 6,513
  • 3
  • 28
  • 50
  • Actually, [Random.nextInt(int)](http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextInt(int)) returns an int between 0 (inclusive) and the given int *exclusive*. So your solution is wrong. – Étienne Miret Apr 08 '13 at 14:39
  • @EtienneMiret yes yes it actually crossed my mind a few seconds before seeing your answer.... – Shark Apr 08 '13 at 14:40
  • My A-Level teacher wants us to do it a specific way (the way in my code) without the random function... I did suggest the other way though... Thanks for your help. – user2233480 Apr 08 '13 at 14:46
  • Why not suggest a monte-carlo simulation then, based on the enthropy of the power fluctuation on the mobo... ? You need to be clear about your **tasks**, **requirements**, **expectations** and **demands** At one point you will need to make a random decision, whether it's a coin-toss or something else, and you will need to use it to generate the said number. So, if your A-Level teacher is assigning you to make a random number generator without random() function, then you need to tell him that he's being an idiot for no reason to you guys. Or, maybe he just wants to see the "forumula" way. – Shark Apr 08 '13 at 14:48
1

For creating random numbers between (including) min and max, you can do this:

Math.floor(Math.random() * (max - min + 1)) + min

Edit: The JAVA tag was added only after I suggested this; before it had no tags hinting at a specific language at all – so that there might be better/already implemented methods for this in language X is well possible. This is a very generic approach.

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • Don't do this, as it biases the numbers needlessly. Instead use `Random.nextInt(int)`. – Joey Apr 08 '13 at 14:37
  • @Eric, uhm, no? `random()` yields a number in [0, 1), scaling this will result in a number in [0, *max* − *min* + 1). Flooring that will yield a number in [0, *max* − *min*] ⊂ ℕ. Adding *min* results in a number from [*min*, *max*] ⊂ ℕ. Rounding errors cannot happen here as they cannot cross integral numbers. While scaling `Math.random()` is the wrong choice here it is for other reasons and has nothing to do with the interval. – Joey Apr 08 '13 at 14:40
  • @Joey, IMO - The only downside to using this method is that you cannot safely share the seed between multiple clients. Seeing how we're nowhere near that usecase - there is really no need for all this nitpicking about "biasing numbers". After all, we're just doing someone's homework now. – Shark Apr 08 '13 at 14:42
  • @Shark: Well, there is a good method for obtaining pseudo-random numbers in a range in Java and it's the one you proposed. The usual (inferior) alternatives are either naïve modulus or floating-point scaling (as here). Both don't ever need to be used for that purpose and I'd rather educate people about it before they commit something to memory which won't perceptibly change results for rolling a die but might screw up numerical simulations half a decade later. – Joey Apr 08 '13 at 14:49
  • @Joey usually it's the teachers fault for not letting them use components made for purposes X, but force them to solve X using A, B and Y. I applaud the education attempt as it's well founded. The problem is, kids are mostly being taught by people that spent their entire 'programming' careers writing bubble-sort code and list traversals... – Shark Apr 08 '13 at 14:52
  • @Joey: I was concerned that the rounding of *max* - *min* + 1 to a representable value, which is required during expression evaluation, would round up to the endpoint of the interval. I do not know what you mean that rounding errors cannot cross integral numbers; `1./3*3` produces `1` even though `1./3` is less than 1/3, so the error at least reaches an integer, which would be a problem if it could happen in scaling the interval. – Eric Postpischil Apr 08 '13 at 15:02
  • @Joey: However, the greatest value `random` can return is 1-e/2, where e is the ULP of 1. If s is the scale (max-min+1), the mathematical result is s-e•s/2. Let p be the greatest power of 2 not greater than s. The mathematical result is (s/p)•p-(s/p)•(p•e/2), and 1≤s/p<2. So the high bit of this has value p, and its ULP is p•e, so (s/p)•(p•e/2) is more than 1/2 of its ULP, so it must round down. (Assuming 0 < s, and s is representable.) So, you are correct, rounding in scaling cannot produce the high endpoint of the interval. (What about a high value of min and rounding in the final addition?) – Eric Postpischil Apr 08 '13 at 15:19