System.Random is not actually randomising

Currently I have two seperate dictionaries, one for racial ethnicity and one for country. Keys in both dicts are numbered 0 to length of dictionary whilst the Values are the actual races/countries themselves.

In a seperate class, I am utilising System.Random to select a number between 0 and the maximum length of the dictionary in order to call a random value from each one in order to be given a random race and random country upon start-up. However, I have now discovered that the race/country pairs are appearing the same every time.

In the code below, I am noticing that Caucasian is always paired with England and Asian is always paired with France. There are others but this is evidence that my randomiser just isn’t working as I’ve never seen Caucasian paired with anything other than England. I’m at a complete loss here, can anyone help me make these pairs ACTUALLY random?

Thanks in advance!


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

static class Lists
{
    public static Dictionary<int, string> countries = new Dictionary<int, string>()
    {
        { 0, "England" },
        { 1, "Italy"},
        { 2, "France" },
        { 3, "Spain" }
    };

    public static int countrieslength = countries.Count;

    public static Dictionary<string, string> denonyms = new Dictionary<string, string>()
    {
        {"England", "English" },
        {"Italy", "Italian" },
        {"France", "French" },
        {"Spain", "Spanish" }
    };

    public static Dictionary<int, string> races = new Dictionary<int, string>()
    {
        {0, "Caucasian" },
        {1, "African-American" },
        {2, "Hispanic" },
        {3, "Asian" },

***

using System;
using System.Threading;
using UnityEngine;

public class Character : MonoBehaviour
{
    public string Race;
    public string Country;
    public string CountryDenonym;

    System.Random r_countries = new System.Random();
    System.Random r_races = new System.Random();

    void Start ()
    {
        Race = Lists.races[r_races.Next(0, Lists.raceslength)];

        Country = Lists.countries[r_countries.Next(0,Lists.countrieslength)];

        CountryDenonym = Lists.denonyms[Country];
    }

Hey there,

I’m not too familiar with the uses of System.Random but may have some sort of solution.

public class Character : MonoBehaviour
 {
     public string Race;
     public string Country;
     public string CountryDenonym;
 
     void Start ()
     {

         int randomRace = Random.Range(0, races.Count)
         Race = Lists.races[randomRace]

         int randomCountry = Random.Range(0, countries.Count)
         Country = Lists.Country[randomCountry]
 
         CountryDenonym = Lists.denonyms[randomCountry];
     }

Well, your issues is that you initialize two Random instances at the same time. The Random class is initialized with the current system time and date. If you create them essentially at the same time chances are really high that both will receive the same seed. So both of your pseudo random number generators will produce the same sequence. Is there any reason why you create two seperate generators? Why don’t you use the same for everything?