Created
April 24, 2021 17:31
-
-
Save TKDKid1000/1546b967252ddab4a534d07d60fcf48f to your computer and use it in GitHub Desktop.
A simple way to create smooth random numbers in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class NumberUtil { | |
public static int getRandomNumberRange(int min, int max) { | |
Random random = new Random(); | |
return random.nextInt(max - min) + min; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class SmoothRandom { | |
private int low; | |
private int high; | |
public SmoothRandom(int low, int high) { | |
this.low = low; | |
this.high = high; | |
} | |
public List<Integer> generate(int maxamount, int change) { | |
List<Integer> out = new ArrayList<Integer>(); | |
int val = (high/2)+low; | |
for (int x=0; x<maxamount; x++) { | |
int rand = NumberUtil.getRandomNumberRange(val-change, val+change); | |
while (rand > high || rand < low) { | |
rand = NumberUtil.getRandomNumberRange(val-change, val+change); | |
} | |
out.add(rand+val); | |
} | |
return out; | |
} | |
public List<List<Integer>> generate3d(int maxX, int maxY, int change) { | |
List<List<Integer>> out = new ArrayList<List<Integer>>(); | |
List<Integer> line1 = generate(maxX, change); | |
out.add(line1); | |
for (int y=0; y<maxY; y++) { | |
List<Integer> line = new ArrayList<Integer>(); | |
out.get(out.size()-1).forEach(number -> { | |
int rand = NumberUtil.getRandomNumberRange(number-change, number+change); | |
while (rand > high || rand < low) { | |
rand = NumberUtil.getRandomNumberRange(number-change, number+change); | |
} | |
line.add(rand); | |
}); | |
out.add(line); | |
} | |
return out; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment