Search Unity

True Random PRO - Real randomness for Unity

Discussion in 'Assets and Asset Store' started by Stefan-Laubenberger, Feb 20, 2017.

  1. MrIconic

    MrIconic

    Joined:
    Apr 5, 2013
    Posts:
    239
    Any idea why this is happening?

    imagemissing.png

    Happens when I have the config window open (infinitely) or a few times when I look at a demo.
     
  2. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    Hi

    Did you restart Unity?


    Cheers
    Stefan
     
  3. MrIconic

    MrIconic

    Joined:
    Apr 5, 2013
    Posts:
    239
    Didn't try that. Removed it because it was warning me hundreds of times.
     
  4. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    Please give it another try.

    The problem is that: True Random copies after the installation all Editor UI-icons to "Editor Default Resources" and sometimes Unity simply doesn't trigger the copy-routine. Unfortunately, we didn't found the issue of the problem (which happens infrequently and behaves different on various Unity-versions).

    However, hitting "Play" or restarting Unity has always solved the problem you encountered.
     
  5. MrIconic

    MrIconic

    Joined:
    Apr 5, 2013
    Posts:
    239
    I will soon. Thanks for the help.

    I do have two questions though:

    1.
    When I first installed the asset I realized that you're using the random.org api. Unfortunate it doesn't seem to be mentioned but with 1 million bits per day allowance it doesn't bother me as much.

    However, I noticed it says "Pricing information comes into effect when the free beta ends on 30 April 2019." on top of the pricing page (on random.org). I assume that has to deal with API calls only. Is there a way to add API keys?

    2.
    Is PRNG unity's random class integrated into this asset or something else?
     
  6. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    Oh, I found the problem... Somehow, the UI icons didn't end up in the release :-(
    Please send me your invoice to get the fixed package.

    About your questions:

    1. Random.org has a beta that is now running for years ;) as soon as they really deprecate their free tier, we will either support API-keys or roll our own solution.
    2. If TR can't reach the Random.org-server or the quota is 0, we use C# System.Random.
     
  7. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    353
    Heyo, just picked this asset up.
    A few questions for you @Stefan-Laubenberger
    - I mainly got this asset to replace all of my standard Random.Range calls with True Randomness calls. Is this overkill?
    - I read something in an earlier forum post about an active Internet connection being required. Does this mean that the asset will fail if the user is offline?
     
  8. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    Hi

    You could do that, but since TR is asynchronous, you would encounter some delays, which is depending on the use-case not optimal.

    Therefore, we recommend the following approach:
    At startup of your app, get an integer (or list of integers) with the "GenerateInteger"-method. Then use it as seed-parameter for the "GenerateXYPRNG"-methods.
    For more, please see the API.

    This is no longer totally true random, but much better than the standard PRNG; another advantage is that you can access the result (=return value) immediately without listening to "onGenerateXYFinished".

    I hope this helps you further.


    Cheers
    Stefan
     
    Last edited: Jul 23, 2019
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Sorry but this asset smells.
    Year later and still didn't mention on main page, that asset requires third party service. And what's more important an internet. So how offline / with no access to internet games suppose to function?
     
  10. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    The function is clearly outlined in the description - it also states what happens when there is no connection available:
    upload_2019-7-23_13-25-21.png

    So nothing "smells" here.
     
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    New asset store kicks me off, from viewing your asset page.
    So I rely on information on the forum.

    However, since you provided screenshot from asset store, still one information is required, that you rely on third party service(s). Wouldn't be that important to highlight?
     
  12. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    It's no secret that we use https://www.random.org/ but you're right, I will add it to description in the next update.

    Thank you and have a nice day!
     
    Antypodish likes this.
  13. OwlTheoryMusic

    OwlTheoryMusic

    Joined:
    Jun 21, 2018
    Posts:
    39
    Nice work, Stefan!
     
    Stefan-Laubenberger likes this.
  14. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    Thank you!
     
  15. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    353
    Thanks for the support @Stefan-Laubenberger . That makes sense. Excited to start using this in our game :D
     
    Stefan-Laubenberger likes this.
  16. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    You're welcome!
    If you have any further questions, don't hesitate to ask.

    Cheers
    Stefan
     
  17. xKAMRONx

    xKAMRONx

    Joined:
    Apr 1, 2017
    Posts:
    3
    After reading a lot about True Random, it appears it requires an internet connection to function. Does it only need an internet connection at the start, or every time it needs a random number?
     
  18. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    Hi

    TR works without Internet, but then it uses the standard PRNG.
    If you have Internet, there are basically two modes:
    1. Initalize the PRNG with a real random seed. This can be done once or as often as needed.
    2. Get all numbers/strings from the Internet
    If there is no longer a connection available, the PRNG is used.

    Does that help you further?


    Cheers
    Stefan
     
  19. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
  20. rizwanbabar693

    rizwanbabar693

    Joined:
    Jul 15, 2016
    Posts:
    17
    I'm kinda having problem understanding the working of random integers generation. I basically need it for my LUDO game so I was wondering if there is easier and cleaner way using this to get the random generated vale into an integer variable like Unity's Random:

    Code (CSharp):
    1. int steps;
    2. steps = UnityEngine.Random.Range(1, 7);
    Because I just want a single vale out of the generator not a list and then make it equal to steps int variable. Help will be appreciated.
    Thanks
     
  21. rizwanbabar693

    rizwanbabar693

    Joined:
    Jul 15, 2016
    Posts:
    17
    I managed to do it but it takes too long to generate. Doesn't seem to suit my need for LUDO game
     
  22. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    Hi

    Yes, there is a small delay and we don't recommend using True Random all the time and for all random operations. What we recommend is this: get like 100 real random numbers at the beginning of the game and use it as seed for the PRNG. Then you can change the seed e.g. every minute. Even that makes it much better than the normal PRNG without a real seed.


    Cheers
    Stefan
     
  23. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    If I had to point out how the tool might be useful, I would say that it's not its main feature, but what seems to be the sub-features (or shortcuts) it procures. If you understand how Unity generates its random values and how to "manipulate" it, it's relatively simple to counter its "pseudo-random" side.

    The one main and only reasonable reason of using an outside (online) service to get a part of the system to generate a "true" random value is only about adding an additional layer of complexity against data manipulation and even that is not much of a defense against anyone who knows what they are doing. (Having the data generated locally can be analyzed locally more easily than having a part of the data generated remotely, but then having the data generated remotely analyzed and reproduced can counter the defense effectiveness.)

    For example, mixing both System.Random with UnityEngine.Random together basically render the pseudo-randomness into a 1/(10^7) of the same variance from using only one of them. That's because while both System.Random and UnityEngine.Random uses pseudo-randomness (which is based on the PC's internal clock value as the main seed of the value generated), they both uses a different overlapping sequence. As such, you can get a semi-true randomness with basically 1/(10^7) chance of getting the same value again by using the same code at the exact same time (and frame) in 2 different days which is as much random as the method used by random.org and other similar system.

    Here's an example of the code to do this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4.  
    5. public class Randomness : MonoBehaviour
    6. {
    7.     private Random.State R_StateHolder;
    8.     private int ValueHolder_Int = 0;
    9.     private float ValueHolder_Float = 0;
    10.  
    11.     public int GetRandomInt(int min, int max){
    12.        ValueHolder_Int = 0;
    13.         R_StateHolder = UnityEngine.Random.State;
    14.         UnityEngine.Random.State = System.Random.Range(0,99999);
    15.        ValueHolder_Int = Mathf.RoundToInt(UnityEngine.Random(min,max));
    16.         return ValueHolder_Int;
    17.         UnityEngine.Random.State = R_StateHolder;
    18.     }
    19.    
    20.     public float GetRandomFloat(float min, float max){
    21.        ValueHolder_Float = 0f;
    22.         R_StateHolder = UnityEngine.Random.State;
    23.         UnityEngine.Random.State = System.Random.Range(0,99999);
    24.         ValueHolder_Float = Mathf.Round(UnityEngine.Random(min,max));
    25.         return ValueHolder_Float;
    26.         UnityEngine.Random.State = R_StateHolder;
    27.     }
    28. }
    The reason why this would work as a counter to the pseudo-randomness of each individual random method is simple:
    The "clock" time set for UnityEngine random value is based on the time when the Unity client is launched while the "clock" time of the System random value is based on the time when the PC was powered up.
    Hence, the only way to counter this case of pseudo-randomness would be to have another PC or device to manipulate the "master PC" that runs the game to power it up and launch the Unity client at specific time frame.

    It's also possible to use another method such as the Security.Cryptography.
    This is an actually "true random" value generator already available inside of Windows. (I haven't tried it on iOS or Android or others yet.)

    This is an example of how I would do it:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Security.Cryptography;
    4.  
    5. public class Randomness : MonoBehaviour
    6. {
    7.     private static RNGCryptoServiceProvider rngCsp;
    8.  
    9.     public void Awake()
    10.     {
    11.         Debug.Log(GetRandomInt(50,100));
    12.         Debug.Log(GetRandomFloat(0.2f, 0.8f));
    13.     }
    14.  
    15.     public static int GetRandomInt(int min, int max)
    16.     {
    17.         int Result= 0;
    18.         rngCsp = new RNGCryptoServiceProvider();
    19.         int[] results = new int[0];
    20.  
    21.         if (min > max)
    22.         {
    23.             results = new int[min - max];
    24.         }
    25.         else
    26.         {
    27.             results = new int[max - min];
    28.         }
    29.  
    30.         byte roll = RollDice((byte)results.Length);
    31.         Result = roll + min;
    32.  
    33.         rngCsp.Dispose();
    34.         return Result;
    35.     }
    36.  
    37.     public static float GetRandomFloat(float min, float max)
    38.     {
    39.         float Result = 0f;
    40.         rngCsp = new RNGCryptoServiceProvider();
    41.         int[] RandomState = new int[100];
    42.         byte CurrentRandomState = RollDice((byte)RandomState.Length);
    43.         UnityEngine.Random.State CurrentStateHolder = UnityEngine.Random.state;
    44.         UnityEngine.Random.InitState(CurrentRandomState);
    45.         Result = UnityEngine.Random.Range(min, max);
    46.         UnityEngine.Random.state = CurrentStateHolder;
    47.         rngCsp.Dispose();
    48.         return Result;
    49.     }
    50.  
    51.     public static byte RollDice(byte numberSides)
    52.     {
    53.         if (numberSides <= 0)
    54.         {
    55.             Debug.LogError("Number of size is below 1. It's currently at " + numberSides);
    56.             return 0;
    57.         }
    58.  
    59.         byte[] randomNumber = new byte[1];
    60.         do
    61.         {
    62.             rngCsp.GetBytes(randomNumber);
    63.         }
    64.         while (!IsFairRoll(randomNumber[0], numberSides));
    65.         return (byte)((randomNumber[0] % numberSides) + 1);
    66.     }
    67.  
    68.     private static bool IsFairRoll(byte roll, byte numSides)
    69.     {
    70.         int fullSetsOfValues = Byte.MaxValue / numSides;
    71.         return roll < numSides * fullSetsOfValues;
    72.     }
    73. }
    74.  
    I made this example based on the Dice Roll code available in the docs on Microsoft .NET website here.

    The GetRandomFloat code is different because this method is limited, natively, to integer values, hence to save on the memory usage of the function, I used a small trick of setting Unity's pseudo-random state-based value with an actual random state, hence making it no more pseudo-random. To avoid too much of an impact on the Garbage Collector, I set it with 100 possible true random state values, but it could be reduced to less or raised to more.

    Now, what I'm trying to express here is not that this tool in the Asset Store is useless, but more that it offers sub-tools that gives some good and simple tricks to overcome some hassles from handling random values. (Such as getting a unique non-linear sequence of random values within a range.)
     
    Stefan-Laubenberger likes this.
  24. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    Hi Max

    Thank you for your nice examples!

    The main difference is the distribution of pseudo-random vs. true-random. An example, if you get 100 picks of the numbers 1 - 10 with a pseudo-generator, every number will be picked 9-11 times. With "true random" it's possible that you pick 100 times the number 1.
    However, I see your point with the two random functions, which will counter it a bit, but it's still not a real random number. A computer is a deterministic machine and can never generate real random numbers - even the mentioned "RNGCrypto".

    The main reason for our tool isn't security - it's used where you want realistic numbers, like in your mentioned "DiceRoll".


    Cheers
    Stefan
     
  25. Rand_D

    Rand_D

    Joined:
    Oct 24, 2017
    Posts:
    44
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4.  
    5. public class Randomness : MonoBehaviour
    6. {
    7.     private Random.State R_StateHolder;
    8.     private int ValueHolder_Int = 0;
    9.     private float ValueHolder_Float = 0;
    10.  
    11.     public int GetRandomInt(int min, int max){
    12.        ValueHolder_Int = 0;
    13.         R_StateHolder = UnityEngine.Random.State;
    14.         UnityEngine.Random.State = System.Random.Range(0,99999);
    15.        ValueHolder_Int = Mathf.RoundToInt(UnityEngine.Random(min,max));
    16.         return ValueHolder_Int;
    17.         UnityEngine.Random.State = R_StateHolder;
    18.     }
    19.  
    20.     public float GetRandomFloat(float min, float max){
    21.        ValueHolder_Float = 0f;
    22.         R_StateHolder = UnityEngine.Random.State;
    23.         UnityEngine.Random.State = System.Random.Range(0,99999);
    24.         ValueHolder_Float = Mathf.Round(UnityEngine.Random(min,max));
    25.         return ValueHolder_Float;
    26.         UnityEngine.Random.State = R_StateHolder;
    27.     }
    28. }
    [/QUOTE]

    Hi, this code does not work at all.
     
  26. WEEE008

    WEEE008

    Joined:
    Sep 19, 2020
    Posts:
    5
    Hello,
    I am having issues as when I generate a number it is in the form e.g. da537df1-6c9a-4dba-828f-976a52582b82
    when I would like to have it in decimal, is there something to fix this?
     
  27. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    Hi

    I apologize, but I don't get the question... What you seem to create is a UID, which is already as random as it gets and TR isn't needed for that.
    Can you please explain in more detail what exactly you wanna achieve?


    Cheers
    Stefan
     
  28. WEEE008

    WEEE008

    Joined:
    Sep 19, 2020
    Posts:
    5
    Hi Stefan, I would like to generate a random integer (in decimal) which I can then use as an input for another function
     
  29. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    Thank you for the details!
    You can use True Random like this:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using Crosstales.TrueRandom;
    4.  
    5. public class SimpleIntegerExample : MonoBehaviour
    6. {
    7.    #region Variables
    8.  
    9.    public int Number = 1;
    10.    public int Min = -1_000_000_000;
    11.    public int Max = 1_000_000_000;
    12.  
    13.    #endregion
    14.  
    15.  
    16.    #region MonoBehaviour methods
    17.  
    18.    private void Start()
    19.    {
    20.       TRManager.Instance.OnGenerateIntegerFinished += onGenerateIntegerFinished;
    21.       TRManager.Instance.OnErrorInfo += onError;
    22.  
    23.       GenerateInt();
    24.    }
    25.  
    26.    private void OnDestroy()
    27.    {
    28.       if (TRManager.Instance != null)
    29.       {
    30.          TRManager.Instance.OnGenerateIntegerFinished -= onGenerateIntegerFinished;
    31.          TRManager.Instance.OnErrorInfo -= onError;
    32.       }
    33.    }
    34.  
    35.    #endregion
    36.  
    37.  
    38.    #region Public methods
    39.  
    40.    public void GenerateInt()
    41.    {
    42.       TRManager.Instance.GenerateInteger(Min, Max, Number);
    43.    }
    44.  
    45.    #endregion
    46.  
    47.  
    48.    #region Callbacks
    49.  
    50.    private void onGenerateIntegerFinished(List<int> results, string id)
    51.    {
    52.       foreach (int result in results)
    53.       {
    54.          Debug.Log(result);
    55.       }
    56.    }
    57.  
    58.    private void onError(string e)
    59.    {
    60.       Debug.LogWarning(e);
    61.    }
    62.  
    63.    #endregion
    64. }
    I hope this helps you further.

    Cheers
    Stefan
     
  30. WEEE008

    WEEE008

    Joined:
    Sep 19, 2020
    Posts:
    5
    Great this works thank you
     
  31. WEEE008

    WEEE008

    Joined:
    Sep 19, 2020
    Posts:
    5
    Would I need the True Random Game Object in the scene as well as the code to make it true random?
     
  32. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    True Random is added automatically to the scene as soon as you use it.
     
  33. WEEE008

    WEEE008

    Joined:
    Sep 19, 2020
    Posts:
    5
    Great thank you
     
  34. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    You're welcome!
    If you like our asset and support, please consider leaving a review in the store.
     
    WEEE008 likes this.
  35. Stefan-Laubenberger

    Stefan-Laubenberger

    Joined:
    May 25, 2014
    Posts:
    1,981
    We've just released version 2024.1.1 of True Random.
    Main changes:
    • Support for Unity Cloud Build improved
    • Updated to Common 2024.1.1