Easy random (Min, Max)

Discussion in 'Plugin Development' started by ChipDev, Jul 27, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ChipDev

    Hi,
    Heres a nice way to make a random int/double with a method you create.. :O

    Int:
    Code:java
    1. public double getRandomDouble(double min, double max){
    2. Random r = new Random();
    3. double randomDouble = r.nextDouble();
    4. double result = min + (max - min) * randomDouble;
    5. return result;
    6. }


    Double:
    Code:java
    1. public int getRandomInt(int min, int max){
    2. Random r = new Random();
    3. int randomInt = r.nextInt();
    4. int result = min + (max - min) * randomInt;
    5. return result;
    6. }
    7.  


    Usage:
    Set a random vector to a entity between -5.0, 5.0 with getRandomDouble.
    Code:java
    1. double random1 = getRandomDouble(-5.0, 5.0);
    2. double random2 = getRandomDouble(-5.0, 5.0);
    3. double random3 = getRandomDouble(-5.0, 5.0);
    4. Piggie.setVelocity(new Vector(random1, random2, random3);
    5.  

    Edit: Thanks for all your feedback.
     
    akabarblake likes this.
  2. Offline

    raGan.

    Instantiating new Random object in every call is not a good thing to do.
     
    skyrimfan1 likes this.
  3. Offline

    Garris0n

    Neither is having syntax errors in your spoonfeeding "resources". Or mislabeling them, for that matter...
     
    ZodiacTheories likes this.
  4. Offline

    raGan.

    tru dat
     
  5. Offline

    ChipDev

    Sure, True.
    There aren't any syntax errors.
    Edit: Oh jee. They just showed up
    Edited!
     
  6. Offline

    raGan.

    The code in second example won't compile. And it's not because of "[ syntax]" tags.
     
    ChipDev likes this.
  7. Offline

    akabarblake

    Yeah... I don't see any.
    >.>
    Garris0n
    Edit:
    @raGan , Didn't mention that because I thought that was a bukkit forums error.
     
  8. Offline

    skyrimfan1

    1. You switched getRandomInt() and getRandomDouble().
    2. The int one returns a double.
    3. It's better to use those as static methods and have them refer to one Random object rather than to create an object per act call.
    4. I'd advocate using a faster random sequence like the MersenneTwisterFast class (3x faster) which also has the benefit of a having a longer period (meaning more randomness!)
    5. Specify whether the min and max values are inclusive or exclusive (inclusive meaning possible to get as a random value, exclusive not)
     
  9. Offline

    raGan.

    You also most probably don't want to multiply number gotten from nextInt() by anything not in <-1, 1>.
     
  10. Offline

    Garris0n


     
  11. Offline

    xTigerRebornx

    ChipDev
    Code:
        public static void main(String[] args) {
           
            System.out.println(getRandomInt(0, 2));
           
        }
       
        public static double getRandomInt(int min, int max){
            Random r = new Random();
            int randomInt = r.nextInt();
            int result = min + (max - min) * randomInt;
            return result;
        }
    Didn't realize
    was in between 0 and 2.
    Also didn't know that doubles are now magically ints.

    Just going to point out, your getRandomInt() method, if it were to return an int, doesn't work. Did you even test this code before posting it?
     
    ZodiacTheories likes this.
  12. Offline

    RawCode

    This forum branch is flooded with invalid misleading *tutorials* from users who don't ever knows JRE basics, including super secret class Math.
    Don't know basic of logic, don't know how primitives are handled.
    Don't know about vanilla wrappers like Double or Integer with lots of features embedded.

    Probably such tutorials posted as attempt to farm "likes", i just can't see any other reason to post something like this.

    Such tutorial only viable if explanation of entropy harvesting, byte filtering or range compression is explained.
    If alternative LCG implementation is provided or something similar.

    Just trying to implement (in invalid way) something already present in JRE is joke.

    well, i will explain more, this time i will provide solid evidence

    1) Allocation of random each time does not make random value more or less random.
    BUT allocation of new random each time drain processor time.
    implementation of entropy harvesting in java will result in exactly same randomness no matter what

    proof included:
    Code:
    	static public void moreRandomIsNoMoreRandomBTW()
    	{
    		//many not soo smart users try to make some value more random by invoking random more times
    		//this WONT make value more random, in fact this makes value LESS random
    		
    		//allocation of new random object each time also wont make value more random
    		
    		//we will keep track of random invocations
    		int[] VALUEA = new int[10];
    		int[] VALUEB = new int[10];
    		
    		Random AHOST = new Random();
    		
    		int A = 0;
    		int TMP = 0;
    		for (;;)
    		{
    			A++;
    			if (A == 1000000) break;
    			TMP = AHOST.nextInt(10);
    			VALUEA[TMP] ++; //care with method inside array handle
    			
    			TMP = new Random().nextInt(10);
    			VALUEB[TMP] ++;
    			
    		}
    		
    		for (int a : VALUEA)
    		{
    			//gauss random of similar check should be performed here but i dont want to make such deep research
    			System.out.println(a);
    		}
    		System.out.println("BREAK");
    		for (int a : VALUEB)
    		{
    			//gauss random of similar check should be performed here but i dont want to make such deep research
    			System.out.println(a);
    		}
    		
    	}
    
    Vanilla (internal) random generate fullset of bytes for value, if some limit defined - required range is extracted from full size.

    all values generated from exactly same pool of bytes, this means - harvesting integer part, then double part have exactly same randomness as trying to extend 0-1 double range.
    BUT extending range cause precision drop, in your case severe drop.

    if you want some dynamic range - generate integer part and then add double part, you will have valid random value.

    valid sample of doubleinrange follows
    Code:
    	static public double randomDoubleInRange(int A,int B)
    	{
    		/*static*/ Random STATIC = new Random();
    		int INTEGERPART = STATIC.nextInt(B-A);
    		double d = STATIC.nextDouble();
    		return d+INTEGERPART+A; //may require to substract 1 here, actually 0.9999 to keep same size-range as vanilla implementations
    	}
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  13. Offline

    ChipDev

    How is this the only reason..?
    I posted this to try and help people (Which you guys clearly made that it Didn't), Not to gain likes.
    (People here are too smart for me.)
     
  14. Offline

    raGan.

    ChipDev
    And that's the main problem. I don't see how this could ever help anyone. Not only is it wrong for all the reasons posted above, the execution is bad as well. As RawCode already told you, maybe you need to learn more yourself before you try to help others.
     
  15. Offline

    xTigerRebornx

    ChipDev Once again, I am going to point out that your method for generating a random int between a range doesn't work. It has a very small change of ever being correct simply due to the fact that you are calling Random#nextInt(), which has no limit what-so-ever and doesn't take the range in consideration.
    From what I can tell, you think that Math#random() and Random#nextInt()return the same thing, and this is not the case. Math#random returns a value between 0.0 and 1.0, Random#nextInt() will return otherwise (it will generate a random int (from all possible int values).
    Main point: Its nice that you are trying to help, but code that doesn't work and is written by someone who doesn't fully understand their own code is more harmful then helpful in most cases. Take time to debug your code before throwing it on here.
     
  16. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Removed an image post and its reply. Moved to a more appropriate subforum. Don't attack the OP; help him.
     
  17. Offline

    ChipDev

    Tested one (Double), thats why int = double. I did this super late. :p
     
  18. Offline

    ZodiacTheories

    ChipDev

    Yep. You always seem to do your resources super late. Suspicious
     
  19. Offline

    ChipDev

    All my good stuff is in the day. then I make terrible recources in the night :p
     
  20. Offline

    ZodiacTheories

    ChipDev

    Seemed to have missed my point :p
     
  21. Offline

    ChipDev

    The point is that:
    Umm... i didn't lie. and I'm not a vampire.
     
  22. Offline

    JBoss925

    Question: Why use this and not just the actual random util?
     
  23. JBoss925 Because for every native util there's a resource to make things "easier" while not actually accomplishing such a task :)

    ChipDev Ever notice how, when programmers make a mistake, a lot of them cover it up with the fact that they were tired? You said it was late when you made this, so that's just a variation of the same excuse. Bare in mind, though, that your audience are, for the most part, programmers too! We notice this excuse, and we are not fooled. Admit you made a mistake and learn from it, rather than making excuses, it's much more productive. :)
     
    JBoss925 likes this.
  24. Offline

    ChipDev

    Of course it is. Terrible to lie, and probably exaggerated.
    But seriously, I was tired.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page