Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Malhalar · Jan 09, 2020 at 11:05 PM · randomdictionaryrandomization

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];
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jan 10, 2020 at 12:15 AM

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Malhalar · Jan 10, 2020 at 01:02 AM 0
Share

I cannot believe I didn't think of that. I was trying to keep everything neat and organised as I will be building upon this in future so thought having two generators would help. I'm such an idiot and you are a complete life saver, thank you!

avatar image
0

Answer by LoadingDegree0 · Jan 09, 2020 at 11:22 PM

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];
      }








Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

124 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random death instantiate? 1 Answer

How to make random number generation more random? 2 Answers

Display random element from dictionary 1 Answer

Random.Range is not changing? 1 Answer

RNG State Changes When Loop Iteration is Skipped? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges