简体   繁体   中英

Generate even numbers 1-4 using Math.random()?

I would like to generate the numbers 1-4 (whole integers) using Math.random. I have only succeeded in getting doubles or large doubles, and cannot figure out how to set a limit on the minimum and maximum.

Math.random(); = something between 0-1 as a double?

I have seen some people suggest something like this: num = Math.random() * 60 + 25; but have no idea what that does, or how it works.

I am not sure if this is a true question, and feel free to let me know if I should delete it.

Edit: Is there a way to not get the numbers to repeat, yet still be random every time the program is run?

int rand = (Math.random() * 4) + 1;

Math.Random is redundant here, use the Random class.

Random rand = new Random();
rand.nextInt(4)+1; //starts at 0, so add 1

Import this class by:

import java.util.*; or import java.util.Random;

the random number in math gives you a decimal number between zero and one. you need to tell it to be within a certain range. something like: (4*Math.random())+1 should give you between 1-4 I think. correct me if I am wrong anyone.

  Random rand = new Random();
  System.out.println(rand.nextInt(4) + 1); // we add 1 because it starts with 0

If you really have to use Math.random you need to multiply (and add). It's quite basic math, Math.random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

So multiplying it with X will give a number greater than or equal to 0.0 and less than X. Cast that to an int to get rid of decimals and if you only want even numbers you can do a few things, the easiest probably being int even = (notSureIfEven >> 1) << 1; . [I'm kind of assuming that with 'even' numbers you actually meant 'whole' numbers though, in which case you can ignore that.] Then if you don't want the range to be 0->X but Y->X you just add Y to your outcome (make sure to subtract Y from X before the multiplication or your range will be Y->X+Y).

To not generate the same number twice you can do different things. One way is to store all the numbers you generated so far in a List and then when you generate a new number, check if the list contains that number already, if so generate a new one until you got one that isn't in the list (and then when you do obviously add that to the list). Another way could be to preload all numbers it could generate into a list and then remove a random number out of that list.

Both ways probably won't scale very well to really large ranges of numbers though. The first one since it might get in a very long loop trying to find a number it hadn't generated yet, the second one because you'll have to create a really large list at the start. I'm not sure if there's something you could do in the case of a really large range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM