Skip to content

Instantly share code, notes, and snippets.

@TKDKid1000
Created April 24, 2021 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TKDKid1000/1546b967252ddab4a534d07d60fcf48f to your computer and use it in GitHub Desktop.
Save TKDKid1000/1546b967252ddab4a534d07d60fcf48f to your computer and use it in GitHub Desktop.
A simple way to create smooth random numbers in java
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;
}
}
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