Jump to content

[1.7.10] World#rand not so random?


jabelar

Recommended Posts

I was trying to add some randomization so my entities would not act in sync.

 

So in the constructor I set some fields to random values using the worldObj.rand from the entity.

 

The entities were not random, so I tested by putting following statement into the constructor:

System.out.println("Rand ="+worldObj.rand.nextInt(6));

 

And as multiple entities were created (from a save game, so all are being constructed in same tick):

[22:10:38] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:initSyncDataCompound:87]: Rand =3
[22:10:38] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:initSyncDataCompound:87]: Rand =3
[22:10:38] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:initSyncDataCompound:87]: Rand =3

 

Basically it looks like the worldObj.rand.nextInt(5) is giving the same value. If I stop and reload the save game, it has same value, and also if I exit completely and run again.

 

However, I get a different value for any I construct later. In other words, all the ones being constructed during loading a game have same value, but ones that get constructed later in the game all have different values.

 

Note that I'm running from Eclipse -- I know some dev environments will fix the seed value to make debugging easier. Is that happening here?

 

Or when a save game is loading, maybe the world's random number generator isn't set up yet?

 

Any other explanation?

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

To analyze why random class gives same value there, it is needed that when and where the Random#nexInt() is called.

So please post the code.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

That does seem odd, but at the same time it is very common for random number generators to generate the same number multiple times in a row (up to 7 times in a row, actually, according to some article I read somewhere...) - it's a common misconception that random means every number is different.

 

Especially with such a small range (0-4), you are bound to end up with lots of repeats, and Random will always output the same sequence of numbers given the same seed.

 

Whether that means you don't have an issue or not, I don't know, but it is very much within the realm of randomness to get numbers that don't seem quite random.

Link to comment
Share on other sites

I already posted the code.  It is in the entity constructor.

 

it is not just the number happening to be the same due to random chance.  It returns same number no matter how many times it runs.  I've got 5 entities in the save game and they all print same value, over multiple save then load cycles,  including exiting fully then restarting.

 

I'm pretty familiar with random generation in other languages and even with fixed seed I think next int method should retuRN different numbers on each subsequent call.

 

The code can produce different numbers because entities constructed later (with spawn egg) have different values. Only those constructed during loading of saved game have same value.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

I already posted the code.  It is in the entity constructor.

 

it is not just the number happening to be the same due to random chance.  It returns same number no matter how many times it runs.  I've got 5 entities in the save game and they all print same value, over multiple save then load cycles,  including exiting fully then restarting.

 

I'm pretty familiar with random generation in other languages and even with fixed seed I think next int method should retuRN different numbers on each subsequent call.

 

The code can produce different numbers because entities constructed later (with spawn egg) have different values. Only those constructed during loading of saved game have same value.

The whole code in the entity constructor is needed.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

The world random object has a seed. Which makes it predictable in behaviour.

 

Thats why maps with a seed spawn the same stuff on different computers.

 

If you want more randomness construct your own Random object and use that. That way it operates without the seed constraints.

 

How much wood could a woodchuck chuck if a wood chuck could chuck wood - Guybrush Treepwood

 

I wrote my own mod ish... still a few bugs to fix. http://thaumcraft.duckdns.org/downloads/MagicCookies-1.0.6.4.jar

Link to comment
Share on other sites

The world random object has a seed. Which makes it predictable in behaviour.

 

Thats why maps with a seed spawn the same stuff on different computers.

 

If you want more randomness construct your own Random object and use that. That way it operates without the seed constraints.

You missed the point of this issue.

Randoms with same seed gives same values in same time, but in this case it gives same values for different times!

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

Is it possible that the entity code is in a different thread?

That way it could be possible they are asking the same random state at the same moment which would result in the same nextInt to be returned in the randomness step.

 

how rand works is

 

Rand(seed) will always return the same next values in order

Nextint 2

Nextint 4

Nextint 7

 

Is it possible that the stepvalue gets reset in the world object or that it gets cloned for the tick event? As not to mess up worldgen?

 

That would also explain the same values because step will always be the same.

 

Future steps will be different because they might happen in seperate ticks.

 

There are multiple possibilities why the random is the same.

How much wood could a woodchuck chuck if a wood chuck could chuck wood - Guybrush Treepwood

 

I wrote my own mod ish... still a few bugs to fix. http://thaumcraft.duckdns.org/downloads/MagicCookies-1.0.6.4.jar

Link to comment
Share on other sites

Is it possible that the entity code is in a different thread?

 

I agree that that is what it seems to act like -- like multiple rands are starting with same seed. But I'm pretty sure Minecraft isn't multi-threaded like that.

 

I don't know if anyone else can replicate the issue. It is pretty simple -- take a custom entity and add line of code to print out random int in constructor, then create multiple entities and save and load the game and see what prints out.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

The whole code in the entity constructor is needed.

 

It is really simply this (other variations yield similar results).

 

public EntityBirdOfPrey(parWorld)
{
    super(parWorld);
    System.out.println("Rand ="+parWorld.rand.nextInt(6));
}

Oh, and how did you created the multiple instances of the EntityBirdOfPrey?

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

World#rand has NOTHING to with worldgen. The random used for worldgen is in ChunkProviderGenerate and has indeed a fixed seed.

 

Any ideas about my problem though? Even with a fixed seed, my results don't make sense (unless the rand instance is somehow cloned or reset to the seed at the time of each constructor).

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Can you print out the seed before you call nextInt?

 

There is no getSeed() function but I think toString() shows the seed so I printed that out.

 

Here is what my constructor looks like currently:

    public EntityBirdOfPrey(World parWorld)
    {
        super(parWorld);
        
        // DEBUG
        System.out.println("EntityBirdOfPrey constructor(), "+"on Client="
                +parWorld.isRemote+", EntityID = "+getEntityId()+", ModEntityID = "+entityUniqueID);

        setSize(2.0F, 3.0F);
        // DEBUG
        System.out.println("World random seed = "+worldObj.rand.toString());
        randFactor = worldObj.rand.nextInt(10);
        // DEBUG
        System.out.println("randFactor = "+randFactor);
        initSyncDataCompound();
        setupAI();
     }

 

Here is console output for the entities constructed from loading the save game:

[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=false, EntityID = 26, ModEntityID = 9b21931b-67ff-4ae4-be5c-6503abe1c41d
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@5905e924
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 1
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=false, EntityID = 27, ModEntityID = 691ba2b6-afe7-4370-9bd4-3a64a01a861b
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@5905e924
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 1
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=false, EntityID = 39, ModEntityID = cf32a9a0-af33-4e24-a920-15dd1926bfb2
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@5905e924
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 1
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=false, EntityID = 40, ModEntityID = 64fbcf89-c902-4820-af27-9779c9c1dd98
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@5905e924
[17:16:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 1

 

Possibly relevant is console ouptut for entities construction on the client side (it seems to be properly random):

[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=true, EntityID = 519, ModEntityID = e2af7ddc-47fa-47c9-b345-cad610ed0b2e
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@19d4ee95
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 0
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=true, EntityID = 523, ModEntityID = b5a59dc1-f32e-4235-9171-30d6396a9c82
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@19d4ee95
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 5
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:70]: EntityBirdOfPrey constructor(), on Client=true, EntityID = 526, ModEntityID = 948012db-6716-41fe-9e91-c120f708f8c4
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:75]: World random seed = java.util.Random@19d4ee95
[17:16:39] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.wildanimals.entities.birdsofprey.EntityBirdOfPrey:<init>:78]: randFactor = 2

 

In both cases (server and client) the seed is constant but only on client does nextInt seem to give random number...

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

That's not the seed, that's the default hashcode for the object ;)

Make a breakpoint and look at the object.

 

Okay, that makes sense that it is the hashcode. Although I think a constant hashcode would indicate that the seed is constant, right?

 

With breakpoint on the line that does next int, the seed for the world rand went like this as it went through the server constructors (these four are being loaded from save game):

 

93667606441033

93667606441033

93667606441033

93667606441033

 

then in the client constructors:

 

139379158755069

195327285708415

89798151390929

154026912586867

 

Then if I create the entity later, on the server seed is:

182302737784538 (now it is different!)

 

And on client:

182192877199158

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Okay, that makes sense that it is the hashcode. Although I think a constant hashcode would indicate that the seed is constant, right?
No, it just means that the object is constant. But objects are mutable.

 

The only place I see that sets the seed for the world Random is some stuff in the structure generation. That shouldn't cause the seed to be constant...

As a workaround you could use the rand field in Entity instead.

 

I ended up just using a new Random since it is a one-off type of thing, but using entity rand is good idea -- I checked and it works properly.

 

I mostly wanted to understand the issue because I feel like it is fairly natural to want to do something random in entity constructor.

 

The last thing that surprises me though is that I thought a set seed makes the sequence predictable but I thought there is still a sequence -- doesn't nextInt() generate different values even if seed is constant? Calling nextInt() several times on the same Random should provide different values even if seed is constant, right? It is really acting like the world rand instance is being re-created with a set seed each time.

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

That's just... mind-boggling :D Do you have a git repo so I could check it out?

 

Sure. https://github.com/jabelar/WildAnimalsPlus-1.7.10

 

The EntityBirdsOfPrey class is the one of interest. I have the code now using the entity rand (i.e. it works), but to see what I was complaining about, change it to worldObj.rand instead.

 

Then you have to create several of the entities in game. There is a spawn egg for "eagle" in the creative tab. The randomness will be okay on initial creation, so then you need to save and load the game. When they are reconstructed that's when the problem occurs.

 

Thanks for looking in to it.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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
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.

Announcements



×
×
  • Create New...

Important Information

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