Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question about Random.Range

Discussion in 'Scripting' started by Sweex, Apr 23, 2014.

  1. Sweex

    Sweex

    Joined:
    Jan 26, 2014
    Posts:
    25
    Hi. I have int variable with random range. I set up unity to use random range between 1 and 5 every few seconds. But problem is that unity sometimes uses two same numbers. For example, first it was number 1 and after fre seconds it's again number 1. Is there any way I can correct this?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Sounds like working as intended. In a random sample, you can get repeating values.

    If you want to produce a different value each time, a very simple way could be something like

    Code (csharp):
    1.  int myNum = .....
    2. /.../
    3. int oldNum = myNum
    4. while (oldNum==myNum){
    5.   myNum = Random.Range...
    6. }
    7.  
    8.  
     
  3. Synapse11

    Synapse11

    Joined:
    Apr 18, 2014
    Posts:
    39
    Don't know the script but:

    Roll a random number 1-5
    This is variable x.
    Roll a random number 1-5 - variable Y
    if x=y,
    Reroll a random 1-5
    else
    (the thing you want to do)
     
  4. rutter

    rutter

    Joined:
    Mar 21, 2012
    Posts:
    84
    Do you expect your randomness to behave like a dice roll (where each outcome is available any number of times), or like a deck of cards (where each outcome is available only once)?

    If you want dice, keep using random calls.

    If you want a deck of cards, you'll need to populate a temporary collection and pull from it. I've created some helper scripts that create a "shuffle bag" -- each item in the bag will only be returned once, and the bag will automatically refill itself once it's empty.
     
  5. Sweex

    Sweex

    Joined:
    Jan 26, 2014
    Posts:
    25
    This is my script:

    Code (csharp):
    1. testNumber = Random.Range (1, 5);
    2.         if (testNumber == 1) {
    3.             Invoke("spawner", 1);
    4.         }
    5.        
    6.         if(testNumber == 2) {
    7.             Invoke("spawner2", 1);
    8.         }
    9.  
    10.         if (testNumber == 3)
    11.         {
    12.             Invoke("spawner3", 1);
    13.         }
    14.  
    15.         if (testNumber == 4)
    16.         {
    17.             Invoke("spawner4", 1);
    18.         }
    If testNumber is 1 I call spawner function. Inside spawner function I call again Start() and value of testNumber should again be random, but not again 1, it should be 2, 3 or 4 so I can call other spawner functions. Also number should be available any number of times, just it should not repeat. It should be like 1, 4, 2, 3, 1, 2, 4, etc. It should not be like 1, 3, 4, 4.
     
    Last edited: Apr 24, 2014
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    You want to use hpjohn's advice then.
     
  7. Sweex

    Sweex

    Joined:
    Jan 26, 2014
    Posts:
    25
    Yeah..it's working. I modified it a bit and it's ok now. Thank you guys!
     
  8. gneissler

    gneissler

    Joined:
    Jun 2, 2017
    Posts:
    3
    pick a number between Min and Max
    if the number is bigger or smaller then 50% of the Max print "YES" or "NO".
    "YES" or "NO" stands for 0,1 (the smallest you can roll without having to deal with rolling 20 times the same number).
    hope this Helps.



    void randomDecide()
    {
    int a = Random.Range(0, 2000);
    if (a <= 1000)
    {
    Debug.Log("No");
    }
    else
    {
    Debug.Log("Yes");
    }
    }
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If the OP is still working on this issue 6 years later then I think dreams of developing a game may be out of reach. Plus your solution is nonsensical, sorry.
     
    StarManta likes this.