Seed based character randomization

Hello, I have a game in which I am creating a character creator. I want to add the option of having a completely random roll for a character, but I am not sure on how to apply the seed mechanic to Random.Range method etc. I have multiple of these methods as the character has quite a lot of different randomizations that need to happen, from hair to their nose size.

How would I apply this to use seed as I want users to be able to share seeds between their games?

Also as a side question how could this support showing of the seed as you change items on the character from selections instead of using randomize button?

Currently the randomization works basically like this:

rand = Random.Range(0, myList.Count);
            selection = myList.ElementAt(rand);
            selection.SetActive(true);

Hello there @icxrusluv

If you want to use seeds, you can not use random. You must be sure that every time a seed is used, the same seed must give the same results, so it can not be random at all. I would so something like this:

What seed does is to generate a number using only the seed number. For example, lets say you have 10 different hair and 10 different faces and you want to genereate them from a seed.

First, have 2 arrays head[10] and face[10]. Each element of the array is a different face and different head.

You need to create some kind of formula to convert a seed number to a selection of elements of that array, for example a "stupid but effective way could be:

Seed number: 856    
Some formula to change the number a little.    
Number to use = Mathf.FloortoInt ( ((Seed +4) *23)/7 )    
Number to use: 2826    
2826.toString()    
Select the last number from the string (i dont remeber the code to do that, but its easy do to) : 6
Select the second last number from the string  : 2    
convert 6 into integrer variable    
use head[6]
use head[2]

You can do whaterver you want with the seed number, but must be always the same formula, so one seed gives always the same results.

You need to adapt it to your needs.

A good practice is this:

Imagine you have now 12 heads and 7 faces

Seed: 786

For head:    
786/12
if resut is > 12 , then divide by 12 again, and again and again, until you get a number between 0 and 12, so you can easy select the element from the array heads[12]

For face:
first, i change the number to divide a little:
Seed+3
789/7
if is > 7, divide again, agian...

I expect you to get what i’m trying to explain. You need a formula to select an elemnt form an array, but must be always the same formula.

Random can be used to select a random seed, but not to calculate which face to choose.

BYEE :smiley:

Not the best way but it should work,
This will result in an insanely long seed, but for every customizable part assign an ID (i.e for your nose every size will have an ID, like size 1 is ID-1 and size 2 is ID-2 and so on… for every part), with this you can simply have an array/list of all the different parts in order according to their ID, then when you want to rea a certain character just take that int out of the ID and find it using the array


If you didn’t exactly get what i meant then here is a more usable example :
So, lets say you have 4 types of customization (nose size, hair type, face size and eye size), then every ID will have the same order Nose size-Hair Type-Face Size- Eye Size (you ID will look like 1-2-4-6) so whenever you want you can just take the specified customization for your array/list


It might be a bit of a pain to code, but it will be worth it at the end :slight_smile:

Seed and random don’t really work together if you want to be able to share a seed that always generates the same thing. Due to your random will change what is displayed randomly. You can of course randomly generate a seed and your character creator can then load from that random seed.

In simple terms
Lets say you have 20 noses, 20 hair, 20 eyes, you could have a seed value ranging for those items 00-20 when you create a random seed you can randomise that part of the seed with a Random.Range

[nose][hair][eyes]
After randomly selecting your seed might look like this
010515

Meaning
Load nose 01
Load hair 05
Load eyes 20

You can then read that line with your character creator and load the correct assets. This does make the seed a bit obviously of course and you might want to run it through some encryption to obfuscate it something like a MD5 Hash would give you 1e2b6291204cddf6ea8a89ab25525cd2 which you can then decrypt back to 010515 and load the character

Use Random.InitState(seed)

  • take some random seed (as int number -can be from int.MinValue to int.MaxValue)
  • use it to generate series of pseudo-random numbers (always the same for same seed) → use them to customize character
  • remember that seed number for later use or throw it away

As for second part - if you manually change some aspect of the character, you can’t easily translate that to seed number. Now you have to manually remember settings/changes.

Edit: you can also use “some string seed”.GetHashCode() to recive int number you can use as seed in Random.InitState(seed);