Jump to content

RNG not my friend or I should code this in a different way?


Recommended Posts

So I have a code that runs 2 loops, one to move on the X axis and other to move on the Z axis.


The idea is to check a 3x3 area around the block that triggers the call.


Once I do that, I generator a random number, and if that number is 1, then I cause block updates for the 3 blocks on the Y axis at that coordinates.


Problem is that for some reason that I don't understand, than random number is 1 most time when the X axis is -1 in relation to the time it's 1 when the X axis is 1. And kinda the same on the Z axis.




			for(int xAxis=-1;xAxis<=1;xAxis++){

			for(int zAxis=-1;zAxis<=1;zAxis++){

					int chance = Stuff.randInt(1, chances);

					if (chance==1){

						if(xAxis == -1){ menosUnoX++; }
						if(xAxis == 0){ ceroX++; }
						if(xAxis == 1){ masUnoX++; }
						if(zAxis == -1){ menosUnoZ++; }
						if(zAxis == 0){ ceroZ++; }
						if(zAxis == 1){ masUnoZ++; }

						System.out.println("xAxis is -1: " + menosUnoX + " times - 0: " + ceroX + " times - 1: " + masUnoX + " times");
						System.out.println("zAxis is -1: " + menosUnoZ + " times - 0: " + ceroZ + " times - 1: " + masUnoZ + " times");

					for(int yAxis=-1;yAxis<=1;yAxis++){

					Block checkBlock =  world.getBlockState(pos.add(xAxis, yAxis, zAxis)).getBlock();
					world.scheduleBlockUpdate(pos.add(xAxis, yAxis, zAxis), checkBlock, 0, 1);


					return EnumActionResult.SUCCESS;






Log after 200 times been the random number 1 when the xAxis was -1:


xAxis is -1: 200 times - 0: 89 times - 1: 53 times
zAxis is -1: 148 times - 0: 100 times - 1: 94 times

Link to comment
Share on other sites

That's only 342 iterations, and while 200 / 342 may seem skewed, it is also entirely within the realm of possibility for an RNG. If you want to see a more even distribution, you'd probably have to run 1000s of iterations.


What kinds of values does 'chances' have? Why not check the chance before you loop to avoid the loop entirely if the chance != 1? And if chance == 1, why not get more random numbers for the axis? E.g.

int chance = Stuff.randInt(1, chances);
if (chance == 1) {
    int xAxis = Stuff.randInt(1, 3) - 2; // gives value between -1 and 1, inclusive
    int zAxis = Stuff.randInt(1, 3) - 2; // gives value between -1 and 1, inclusive
    // now iterate over the yAxis as you like using xAxis and zAxis
    return EnumActionResult.SUCCESS;

That way you only have one single loop all on its own instead of nested inside of 2 other loops, and far fewer random number generations, all with effectively the same result.

Link to comment
Share on other sites

The chance is running at that step on the loops because, when the chance is 1, I don't need to update the 3x3x3 blocks in the area, but only the 3 on the Y level that are in X/Z coordinates where chance came as "true" (=1)


chances is random between 1 and 2 in this example. But I run this code, because I noticed since the day I made this code, that the crops on the west were always growing faster than the ones on the east, so I knew something was wrong.


However what I did now, is kinda what you suggested, only run the loop and update the 3x3x3 area at once when chance = 1. It makes crops grow faster and I wont have this "unevenness" problem. The only problem I have now is that when causing updates around cactus and sugarcanes, they all grow at once, but I can live with that :P




Link to comment
Share on other sites

Rather than randomly determine if a location gets an update, randomly determine a location to update.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.


Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.


DO NOT PM ME WITH PROBLEMS. No help will be given.

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.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.