Jump to content

gRandom replacement snippet


Reveance

Recommended Posts

This is a class containing the 'what would have been' gRandom's replacement that was mostly created by @Alek following my bug report. I figured I'd share it as a snippet so it's easier to find in case people need it, since Alek just announced a new release in which it is deprecated because of it not functioning correctly and too many dependencies on it to fix.

Code:

 

import java.util.Random;

public class RandomUtil {

	private Random random;

	public RandomUtil() {
		this(new Random());
	}

	public RandomUtil(Random random) {
		super();
		this.random = random;
	}

	/**
	 * Generates a random integer with normal distribution using the supplied
	 * deviation and mean with a cap based on the 68–95–99.7 rule
	 * 
	 * @param stdDeviation
	 * @param mean
	 * @return integer A random integer with normal distribution using the
	 *         supplied deviation and mean capped based on the 68–95–99.7 rule
	 */
	public int gRandom(int stdDeviation, int mean) {
		return Math.toIntExact(Math.round(gRandom((double) stdDeviation, (double) mean)));
	}

	/**
	 * Generates a random integer with normal distribution using the supplied
	 * deviation and mean with a custom cap
	 * 
	 * @param stdDeviation
	 * @param mean
	 * @param minCap
	 * @param maxCap
	 * @return integer A random integer with normal distribution using the
	 *         supplied deviation and mean capped by supplied values
	 */
	public int gRandom(int stdDeviation, int mean, int minCap, int maxCap) {
		return Math.toIntExact(
				Math.round(gRandom((double) stdDeviation, (double) mean, (double) minCap, (double) maxCap)));
	}

	/**
	 * Generates a random double with normal distribution using the supplied
	 * deviation and mean with a custom cap
	 * 
	 * @param stdDeviation
	 * @param mean
	 * @param minCap
	 * @param maxCap
	 * @return double A random double with normal distribution using the
	 *         supplied deviation and mean capped by supplied values
	 */
	public double gRandom(double stdDeviation, double mean, double minCap, double maxCap) {
		double result;
		do {
			result = random.nextGaussian() * stdDeviation + mean;
		} while (result < minCap || result > maxCap);

		return result;
	}

	/**
	 * Generates a random double with normal distribution using the supplied
	 * deviation and mean with a cap based on the 68–95–99.7 rule
	 * 
	 * @param stdDeviation
	 * @param mean
	 * @return double A random double with normal distribution using the
	 *         supplied deviation and mean capped based on the 68–95–99.7 rule
	 */
	public double gRandom(double stdDeviation, double mean) {
		double max = stdDeviation * 3; // 68–95–99.7 rule

		double result;
		do {
			result = random.nextGaussian() * stdDeviation + mean;
		} while (Math.abs(result - mean) > max);

		return result;
	}

	/**
	 * Returns a random integer based on normal distribution between two
	 * integers
	 * 
	 * @param min
	 *            Minimum integer (inclusive)
	 * @param max
	 *            Maximum integer (inclusive)
	 * @return An integer between min and max (inclusive)
	 */
	public int gRandomBetween(int min, int max) {
		return Math.toIntExact(Math.round(gRandomBetween((double) min, (double) max)));
	}

	/**
	 * Returns a random double based on normal distribution between two doubles
	 * 
	 * @param min
	 *            The minimum double (inclusive)
	 * @param max
	 *            The maximum double (inclusive)
	 * @return A double between min and max (inclusive)
	 */
	public double gRandomBetween(double min, double max) {
		if (max - min == 0) {
			return 0;
		} else if (max - min < 0) {
			min += (max - (max = min));
		}

		double deviation = (max - min) / 6d;
		double mean = (max - min) / 2d + min;

		return gRandom(deviation, mean);
	}

	public Random getRandom() {
		return random;
	}

	public void setRandom(Random random) {
		this.random = random;
	}

}

 

Usage:

RandomUtil randomUtil = new RandomUtil();

randomUtil.gRandomBetween(200, 800);
// Is the same as
randomUtil.gRandom(100, 500);

Edited by Reveance
Updated code
  • Like 2
Link to comment
Share on other sites

1 hour ago, Reveance said:

This is a class containing the 'what would have been' gRandom's replacement that was mostly created by @Alek following my bug report. I figured I'd share it as a snippet so it's easier to find in case people need it, since Alek just announced a new release in which it is deprecated because of it not functioning correctly and too many dependencies on it to fix.

Code:

  Hide contents


import java.util.Random;

public class RandomUtil {

	private Random random;

	public RandomUtil() {
		this(new Random());
	}

	public RandomUtil(Random random) {
		super();
		this.random = random;
	}

	/**
	 * Generates a random integer with normal distribution using the supplied
	 * deviation and mean with a cap based on the 68–95–99.7 rule
	 * 
	 * @param stdDeviation
	 * @param mean
	 * @return integer A random integer with normal distribution using the supplied
	 *         deviation and mean capped based on the 68–95–99.7 rule
	 */
	public int gRandom(int stdDeviation, int mean) {
		return Math.toIntExact(Math.round(gRandom((double) stdDeviation, (double) mean)));
	}

	/**
	 * Generates a random double with normal distribution using the supplied
	 * deviation and mean with a cap based on the 68–95–99.7 rule
	 * 
	 * @param stdDeviation
	 * @param mean
	 * @return double A random double with normal distribution using the
	 *         supplied deviation and mean capped based on the 68–95–99.7 rule
	 */
	public double gRandom(double stdDeviation, double mean) {
		double max = stdDeviation * 3; // 68–95–99.7 rule

		double result;
		do {
			result = random.nextGaussian() * stdDeviation + mean;
		} while (Math.abs(result - mean) > max);

		return result;
	}

	/**
	 * Returns a random integer based on normal distribution between two
	 * integers
	 * 
	 * @param min
	 *            Minimum integer (inclusive)
	 * @param max
	 *            Maximum integer (inclusive)
	 * @return An integer between min and max (inclusive)
	 */
	public int gRandomBetween(int min, int max) {
		return Math.toIntExact(Math.round(gRandomBetween((double) min, (double) max)));
	}

	/**
	 * Returns a random double based on normal distribution between two doubles
	 * 
	 * @param min
	 *            The minimum double (inclusive)
	 * @param max
	 *            The maximum double (inclusive)
	 * @return A double between min and max (inclusive)
	 */
	public double gRandomBetween(double min, double max) {
		if (max - min <= 0) {
			return 0;
		}

		double deviation = (max - min) / 6d;
		double mean = (max - min) / 2d + min;

		return gRandom(deviation, mean);
	}

	public Random getRandom() {
		return random;
	}

	public void setRandom(Random random) {
		this.random = random;
	}

}

 

Random r = new Random();

public double gRandom(double mean, double standardDeviation) {

            return r.nextGaussian()*standardDev + mean; //Regarding the 68-95-99.7 rule, r.nextGaussian() does all of that work for you. All you need is the shift.

}

public int gRandomInt(int mean, int standardDeviation) {

       return (int)gRandom(mean, standardDeviation);

}

public double gRandomBetween(double min, double max) {

         return gRandom((max+min)/2.0, (max-min)/6.0);

         //Puts the mean between the max and the min, will only get values within 99.7% of the distribution

}

public int gRandomBetweenInt(int min, int max) {

       return (int)gRandomBetween(min, max);

}

If I've made a mistake in any of these, someone correct me because I wrote these in like a few minutes, but these should work.

Edited by Imateamcape
Adding in the last 2 methods.
Link to comment
Share on other sites

51 minutes ago, Imateamcape said:

Random r = new Random();

public double gRandom(double mean, double standardDeviation) {

            return r.nextGaussian()*standardDev + mean; //Regarding the 68-95-99.7 rule, r.nextGaussian() does all of that work for you. All you need is the shift.

}

public int gRandomInt(int mean, int standardDeviation) {

       return (int)gRandom(mean, standardDeviation);

}

public double gRandomBetween(double min, double max) {

         return gRandom((max+min)/2.0, (max-min)/6.0);

         //Puts the mean between the max and the min, will only get values within 99.7% of the distribution

}

public int gRandomBetweenInt(int min, int max) {

       return (int)gRandomBetween(min, max);

}

If I've made a mistake in any of these, someone correct me because I wrote these in like a few minutes, but these should work.

I wouldn't recommend using that code since basically all the important stuff from the class I posted is stripped off...

Your command regarding the 68-95-99.7 rule doesn't make sense, since the rule is only used for capping the value, which your method does not do. In theory you could get an infinite wait with your method which you do not want in regards to botting. 

Then the way in which you convert to int by casting is horrible in this scenario...since casting a double to an int is always rounding down, so 5.9 will become 5. This means you will have a more likely chance to get min and you'll almost never get max since it has to be exactly max as a double. 

Edited by Reveance
Link to comment
Share on other sites

1 hour ago, Reveance said:

I wouldn't recommend using that code since basically all the important stuff from the class I posted is stripped off...

Your command regarding the 68-95-99.7 rule doesn't make sense, since the rule is only used for capping the value, which your method does not do. In theory you could get an infinite wait with your method which you do not want in regards to botting. 

Then the way in which you convert to int by casting is horrible in this scenario...since casting a double to an int is always rounding down, so 5.9 will become 5. This means you will have a more likely chance to get min and you'll almost never get max since it has to be exactly max as a double. 

The first method was what I already had, only added in a couple more methods. All of Most of the extra work that you currently have is unnecessary.

Edit: I'd be willing to concede the integer methods (note: wrote this in 2 minutes like i said in my post), though the vast majority of your code is extra.

Edited by Imateamcape
Link to comment
Share on other sites

13 minutes ago, Imateamcape said:

This was what I already had, only added in a couple more methods. All of Most of the extra work that you currently have is unnecessary.

Edit: I'd be willing to concede the integer methods (note: wrote this in 2 minutes like i said in my post), though the vast majority of your code is extra.

It's not really unnecessary though...your between methods return integers outside of the min and max values specified. But tbh I think I just don't really understand why you posted it in the first place then

Link to comment
Share on other sites

7 minutes ago, Reveance said:

It's not really unnecessary though...your between methods return integers outside of the min and max values specified. But tbh I think I just don't really understand why you posted it in the first place then

Read my last post back. Conceded the integer point because I thought that was a reasonable argument, though a lot of the remainder of the code is extra.

Link to comment
Share on other sites

50 minutes ago, Imateamcape said:

Read my last post back. Conceded the integer point because I thought that was a reasonable argument, though a lot of the remainder of the code is extra.

Ah okay, I thought you were only talking about casting the ints since you only mentioned the integer methods and because tbh what else is there? :"D The only thing left then is the min max check

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...