#Weird random not being random bug

1 messages · Page 1 of 1 (latest)

pure current
#

Title. I keep getting 977 every time instead of an actual random number on line 88

delicate pagoda
#

Again, it's always 977? Every time you run the game it does that?

smoky plume
#

could try manually setting the seed

#

maybe to like time ticks

pure current
#

yes. I run play and it outputs 977

#

im going insane ngl lol

delicate pagoda
pure current
#

I don't though

#

I debug it right after its set

#

and even the debug log shows 977

delicate pagoda
#

No, I mean the Unity Random Seed

#

the thing that generates your randoms

#

not your variable seed

pure current
#

Should I use system.random instead? maybe thatll help?

delicate pagoda
#

Somewhere in your code you are calling Random.InitState and giving it the same values

smoky plume
#

Random.InitState((int)DateTime.Now.Ticks);

delicate pagoda
#

If anything in your project sets InitState, it's shared for all randomness in the project

smoky plume
#

as a test try that

#

but yeah i would search the whole project including 3rd party packages for a call to InitState

pure current
#

My Broccoli Tree Creator asset apparently calls
UnityEngine.Random.InitState (srcInt);

smoky plume
#

there is the problem then

pure current
#

so poor asset development then lol

#

damn

pure current
#

oh neverind

smoky plume
pure current
#

lol alright

smoky plume
#

generally since UnityEngine.Random is global best not to have anything mess with its state

pure current
#

that kind of sucks, bought the tree creator asset for $45

smoky plume
#

if you need a instance of a random where you control the seed manually i would use System.Random for that

pure current
#

alright thank you

smoky plume
#

yeah its sucks since the unity one, has a bunch of nice things for gamedev

#

like Random.insideUnitCircle/Sphere

pure current
#

it's still worth the purchase though, i modified the asset and integrated it so it can generate trees procedurally for the entire game

#

one of my main goals was to have a unique tree for every world, every tree

smoky plume
#

its really weird what its doing

#

like what is srcInt in that code?

pure current
#

for the tree creator?

smoky plume
#

more or less what you are seeing is when the seed is a constant and is the same for each run

pure current
#

yeah, i assume it's because the date.now doesnt go into miliseconds

#

and maybe just days

#

this actually explains why some of the trees have very similar shapes and sizes, then very different ones the next day

#

huh

smoky plume
#

well how is it generating the srcInt it seeds random with?

#

show the code

pure current
#
            // Generate consistent color for a type - using string because it delivers greater variety of colors than type hashcode
            int srcInt = (int)(type.AssemblyQualifiedName.GetHashCode ());
            UnityEngine.Random.InitState (srcInt);
            Color = UnityEngine.Random.ColorHSV (0, 1, 0.6f, 0.8f, 0.8f, 1.4f);```
#

ill show a snippet because it is a unity asset store product

smoky plume
#

hmm there must be more places its using it

#

thats a really weird usage since that would be the same seed everytime, looks like they were trying to be too clever

pure current
#

yeah it happens for a few more documents

#

should I try changing it?

#

ive already modified the asset a few times so

smoky plume
#

could do that, or reseed it to something after you know all of them have already executed

#

like my example that used (int)DateTime.Now.Ticks

pure current
#

yeah that would make more sense