Search Unity

Using RandomOps?

Discussion in 'Scripting' started by Ch0nG, Aug 28, 2014.

  1. Ch0nG

    Ch0nG

    Joined:
    Aug 4, 2014
    Posts:
    9
    Update: I got it (partially) working! :D See my second post for how I did it.

    I am trying to use data from Random.org in a game I am making. While I know that I can use something like Random, I found out that someone made a C# library so fresh data from Random.org can be used instead of the Pseudorandom generator built-in to C#.

    I tried to add the library in Visual Studio but the game refuses to load when I run it in Unity.

    The library is available here:
    http://www.hvass-labs.org/projects/randomops/cs/files/RandomOpsCS2_1.zip

    FWIW, here are the instructions for the library:
    1. Unpack the RandomOps archive to a convenient directory.
    2. In MS Visual Studio open the Solution in which you will use RandomOps.
    3. Add the RandomOps project to the solution.
    4. Add a reference to RandomOps in all the projects which must use it.

    I'd appreciate any help on getting this to work.
     
    Last edited: Aug 29, 2014
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    does it have a API you can call? or a download?
     
  3. Ch0nG

    Ch0nG

    Joined:
    Aug 4, 2014
    Posts:
    9
    Ah, yes. Sorry I forgot to link to it. It does not have an API but it does have a downloadable file. I've added the link to the OP.

    Here is what I did to get it working:
    First, I compiled the DLL in Visual Studio.
    Next, I added it as a Reference (Project -> Add Reference -> Browse to DLL).
    NOTE: I also had to move the DLL to the Scripts folder, I'm not sure if there is a way around that.

    Next, I created a test script (the first one below).
    Finally, I added a print to my Player script (the second one below) and it prints out the random data successfully. :)

    However, if I change what is inside the TrueRandom class it gives an error:
    "MissingMethodException: Method not found: 'System.Threading.Monitor.Enter'."

    I'll keep at it and report if/when I figure out a fix.

    Code (csharp):
    1.  
    2. namespace TrueRNG
    3. {
    4.   public static partial class TrueRandom
    5.   {
    6.   public static RandomOps.Ran2 Random =
    7.   new RandomOps.Ran2();
    8.   }
    9. }
    10.  
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using TrueRNG;
    5. public class Player : MonoBehaviour {
    6. private Transform TS;
    7.   public int pspeed = 20;
    8. public static int playerLives = 3;
    9. public static int score = 0;
    10. float timer = 0f;
    11.  
    12.   public GameObject LaserFab; //variable to reference prefab.
    13. // Use this for initialization
    14. void Start () {
    15. TS = transform;
    16.   TS.position = new Vector3(0, -14.78957f, 0); //Spawn point
    17.   for (int i = 0; i < 10; i++)
    18.   {
    19.   print("True random " + i +" is " + TrueRandom.Random.Uniform());
    20.   }
    21. }
    22.  
    23. // Update is called once per frame
    24. void Update () {
    25. TS.Translate(Vector3.right * Input.GetAxis("Horizontal") * pspeed * Time.deltaTime);
    26. if (TS.position.x>11.25f || TS.position.x<-11.25f) {
    27. TS.position = new Vector3 (TS.position.x*-1, TS.position.y, TS.position.z);
    28. }
    29. //Fire a laser with the Spacebar.
    30. if (Input.GetKeyDown("space")) {
    31.  
    32.   //Get pseudo-randomized laser position
    33.   //This works but is not "truly" random...
    34.   float laspo = Random.Range(0, 100) > 50 ? -1.917277f : 1.917277f;
    35.  
    36.   /*
    37.   * float laspo =
    38.   * while (!TrueRandom.Random.IsAvailableUniform())
    39.   {
    40.   print("Waiting for True RNG/Random.org data...");
    41.   } */
    42. //Set Laser position
    43. Vector3 position = new Vector3(TS.position.x+laspo, TS.position.y+3.50f, TS.position.z);
    44. //Fire projectile.
    45. Instantiate(Resources.Load("LaserFab"), position, Quaternion.identity);
    46. }
    47. if (Time.time - timer > 0.4f) {
    48. renderer.enabled = true;
    49. }
    50. print ("Lives remaining: " + playerLives + " Score: " + score + " Current playing time: " + "0");
    51. }
    52. void OnTriggerEnter(Collider collider)
    53. {
    54. if (collider.CompareTag ("Enemy")) { //Tag is name of prefab.
    55. //When the enemy hits the player, destroy the player and the enemy.
    56. playerLives--;
    57. renderer.enabled = false;
    58. timer = Time.time;
    59. }
    60. if (playerLives < 1)
    61. Destroy(this.gameObject);
    62. }
    63. }
     
    Last edited: Aug 29, 2014
  4. Rbert

    Rbert

    Joined:
    Jul 13, 2014
    Posts:
    28
    whats the problem with this code? it wont stop randomizing the value of a button..
    var customGuiStyle : GUIStyle;
    var mySkin : GUISkin;
    var mySkin1 : GUISkin;
    var tex : Texture;


    function OnGUI() {
    var w1="abcdef";
    var w2="ghijkl";
    var top = 50;
    var ind = 0;
    var ind1 = new Array(6);
    var bot=50;


    for(var x=0;x< w1.length;x++){

    //ind1[x]= Random.Range(0,Random.Range(0,5));
    ind1[x] = w1[x];

    }

    for (var i = ind1.length - 1; i > 0; i--) {
    var r = Random.Range(0,i);
    var temp = ind1;
    ind1 = ind1[r];
    ind1[r] = temp;
    }


    for(var z=0;z<=5;z++){
    if(GUI.Button(Rect(200 + top,340,40,40),ind1[z].ToString())) {
    Debug.Log("");
    }
    top+=50;
    }

    for(var y=0;y<=5;y++){
    var zxc = Random.Range(0,5);
    if(y>=5){
    ind=5;


    }
    else{
    ind=y;
    }
    if(GUI.Button(Rect(200 + bot,390 ,40,40),w2[zxc].ToString())) {
    Debug.Log("Clicked Button");
    }
    bot+=50;
    }




    }

    }