-3

I'm writing code for a kind of minigame in another game (Minecraft) in Java and I stumbled upon a problem.

I have two ints, int point 1 and int point 2. They are x coordinates, from ex: -100000 to 100000. But also -10000 to -5000, and 10000 to 20000. So negative to positive, but also negative to negative and positive to positive.

And that is the problem. I need to find a solution how to get random numbers from ex -100 to -50. But the same code has to be used with -100 to 50, and 10 to 50.

If you guys can help me, that'd be great!

Greetings,

Jesse.

PS: If you need a code snippet or something, just say it.

Jesse
  • 11
  • 1
  • 2
  • 4
    Duplicate : https://www.google.ro/search?q=java+generate+random+number+in+range&rlz=1C1OPRB_enRO540RO540&oq=java+generate+&aqs=chrome.2.69i57j0l3.3748j0j2&sourceid=chrome&ie=UTF-8 – Silviu Burcea Oct 29 '13 at 13:29
  • I googled alot and could not find negative to negative examples, this isn't a duplicate. – Jesse Oct 29 '13 at 13:29
  • @Jesse It makers not difference whether the values are negative or positive. The first Google hit of Silviu's search works for all value ranges. – Torben Oct 29 '13 at 13:32
  • @Jesse: Good joke. Random.nextInt(n) generates a number between 0 and n-1. Random.nextInt(50) generates an int in range 0, 49. For -25, 25 range, simply add -25 :) Basicly: Random.nextInt(max - min + 1) + min. – Silviu Burcea Oct 29 '13 at 13:32
  • 1
    It **is** a duplicate.... just use negative Min and/or Max – rolfl Oct 29 '13 at 13:33

2 Answers2

2

Silviu Burcea's comment contains a google search which answers your question. Numbers are numbers. It doesn't matter what sign they have. So going to the first result from the google search gave the answer:

public static void main(String[] args){
    Random rand = new Random();

    for(int j = 0; j < 100; j++){
        // just modified the line from the other thread 
        // which was "int randomNum = rand.nextInt((max - min) + 1) + min;"
        int i = rand.nextInt(-50 - -100 + 1) + -100;
        System.out.println("Next value = " + i);
    }

}

That prints out pseudo random numbers between -100 and -50. So yeah, your question is a duplicate.

MadConan
  • 3,749
  • 1
  • 16
  • 27
  • Your modifications break the code..... The `+1` has to be inside the `nextInt(...)` call. Your answer makes the value `-100` impossible to get. – rolfl Oct 29 '13 at 14:12
  • @rolfl: fixed it. But the cusp of my answer was that what the poster was looking for already existed just as Burcea commented. – MadConan Oct 29 '13 at 14:22
  • That may be, but if you provide example code as a solution, then it should be right (or you should expect to be critiqued) – rolfl Oct 29 '13 at 14:25
  • I'm fine with that, assuming I'm wrong. Technically, his question didn't specify inclusive or exclusive end points, so I *could* argue my answer is fine. But I also see that I inadvertently altered the code I was referencing, so it could be wrong from that standpoint. Indeterminate = -1? – MadConan Oct 29 '13 at 14:29
-1
int mix = Math.min(point1, point2) + (int)(Math.abs(point1 - point2) * Math.random()));

EDIT:

int mix = Math.min(point1, point2) + Math.round(Math.abs(point1 - point2) * Math.random()));

EDIT2 :

int mix = Math.min(p1, p2) + (int)Math.round(-0.5f+(1+Math.abs(p1 - p2))*Math.random());

The distirubtion of this expression evaluation seems to be probably better. But this is not an optimised solution in fact.

dooxe
  • 1,481
  • 12
  • 17