{
for(int k = 0; k < 1; k++)
{
int RandPosX = i + random.nextInt(5);
int RandPosY = random.nextInt(80);
int RandPosZ = j + random.nextInt(5);
(new WorldGenShack()).generate(world, random, RandPosX, RandPosY, RandPosZ);
}
}
Firstly, you can remove the for loop, it doesn't do anything. Just leave the code that's inside it. In answer to your question, the easiest way to decrease the occurrences of your generation is to surround your code with an if statement that checks a random int, like so:
For instance, say the code you provided is called 50 times, approximately how often would you like a structure to actually be built?
import java.util.Random;
public class StructureRarity
{
private static final Random random = new Random();
public static void main(String[] args)
{
int count = 0;
double totalPercentage = 0.0;
for (; count < 15; count++)
{
totalPercentage += simulate();
}
System.out.println();
System.out.println("Total success percentage: " + (totalPercentage / count) + " %");
}
private static double simulate()
{
int attempts = 0;
int structures = 0;
random.setSeed(random.nextLong());
for (int i = 0; i < 50; i++)
{
if ((random.nextInt(15) == 0) && (random.nextDouble() < 0.25))
{
structures++;
}
attempts++;
}
double successRatio = ((double) structures * 100.0) / (double) attempts;
System.out.print("Built a total of " + structures + " in " + attempts
+ " attempts. ");
System.out.println("(" + successRatio + " % success)");
return successRatio;
}
}
Example run:
Built a total of 0 in 50 attempts. (0.0 % success)
Built a total of 0 in 50 attempts. (0.0 % success)
Built a total of 1 in 50 attempts. (2.0 % success)
Built a total of 1 in 50 attempts. (2.0 % success)
Built a total of 1 in 50 attempts. (2.0 % success)
Built a total of 2 in 50 attempts. (4.0 % success)
Built a total of 2 in 50 attempts. (4.0 % success)
Built a total of 0 in 50 attempts. (0.0 % success)
Built a total of 1 in 50 attempts. (2.0 % success)
Built a total of 0 in 50 attempts. (0.0 % success)
Built a total of 1 in 50 attempts. (2.0 % success)
Built a total of 0 in 50 attempts. (0.0 % success)
Built a total of 1 in 50 attempts. (2.0 % success)
Built a total of 2 in 50 attempts. (4.0 % success)
Built a total of 0 in 50 attempts. (0.0 % success)
Total success percentage: 1.6 %
So, you want 15%. 15% is the same as saying '15 out of every 100.' So, using the wonderful thing that is mathematics, or, more specifically, fractions, you can figure out what to replace 'RARITY_INT_HERE' from your first reply with. So, first, you must know some stuff about the Random class. When you do 'random.nextInt(5)' what that is literally doing is picking a random number from 0 to 5-1 (4, of course.) So, it has 5 possibilities: 0,1,2,3,4. Therefore, it has a 1/5 (20%) chance of being a 0 or a 1 or 2 or 3 or 4. Now, all you have to do is make 15% (15/100) have a 1 on top rather than a 15. Just divide the top by 15 and the bottom by 15. So, 15/15=1. 100/15=6.666.... So, for 15%, you get 1/6.66666666.... So, you replace 'RARITY_INT_HERE' with 6.66666.... But wait, that's not an integer. But, there's a few solutions. One would be to pick another number, but that's neither good logic practice nor good programming practice. Another thing you could do is change the if statement. Instead of having it only work with 1 number, make it work with 3. (Because the smallest 15/100 can be divided down into is 3/20.) So, that's what you have to do. Make the if statement work with 3 numbers. However, I'm not going to tell you how to do that. I believe that practice makes perfect and that asking a bunch of random strangers on a forum is cheating. If you really want to be a good programmer (/mod maker/software developer/whatever), you have to figure stuff out with your brain. Not mine.
Your intended 15% spawn was for one whole biome, but I'm guessing the generation is being called more frequently than that. Is it for each chunk? To get exacly 15% it would be = 6.666 * (av. chunks in a biome). To be honest I would just try large numbers until you get a spawn rate you're happy with.
that 15 is for 15% but like the above person said, it is per chunk, not per biome. Say, maybe 100 chunks per biome (just made that up, not based on any actual data) so you should say 100*100 = 10,000 instead of 100 for the random.nextInt(). You might need more or less rarity, I don't know. If you make the 10000 larger, it will be more rare. If you make the 15 larger, it will be less rare.
here is my generation part of my mod_wau
The higher the rarity int is, the fewer times it will execute your code.
For instance, say the code you provided is called 50 times, approximately how often would you like a structure to actually be built?
Example run:
What I mean is, do you want to generate a structure at for example 50% chance when your code is called?
That is the effect of randomness You could find none, one or more
If it spawns too often, change the rarity. That simple.
if(random.nextInt(10000) < 15){
your code;
}
that 15 is for 15% but like the above person said, it is per chunk, not per biome. Say, maybe 100 chunks per biome (just made that up, not based on any actual data) so you should say 100*100 = 10,000 instead of 100 for the random.nextInt(). You might need more or less rarity, I don't know. If you make the 10000 larger, it will be more rare. If you make the 15 larger, it will be less rare.