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

Deterministic Hit Chance

Discussion in 'Scripting' started by Descus, Feb 18, 2020.

  1. Descus

    Descus

    Joined:
    May 10, 2019
    Posts:
    3
    Hey Guys,
    How can i solve a Unit having a 85% hitchance but with a random seed, isnt the unit hitting with the same propability if i dont change the seed ?

    Thanks for Help
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    Your sentence is a bit malformed... but I assume you're asking if the seed is the same, if the random result will be the same.

    And yes.

    If you start with the same seed, then the random sequence it generates is deterministic. So if you calculate the 85% chance on both simulations with the same seed at the same exact iteration of the sequence, it'll be the same.
     
  3. Descus

    Descus

    Joined:
    May 10, 2019
    Posts:
    3
    So i need to change the Seed after every hit but need to save them in order to reconstruct the scenario ?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    What?

    No, you don't need to change the seed after every hit.

    What are you trying to ask? Can you rephrase your first question so I can understand your needs?
     
  5. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    If you initialize your random object with a seed every time you start the game, yes, the unit always hits in the same pattern.

    You can avoid this by storing the state of the Random, see Random.state. If I recall correctly, you can serialize it. When loading your game, set Random.state to the deserialized state from the previous session.

    It is worth mentioning that if you have a lot of units attacking hitting based on a probability, it is highly unlikely that the player will notice what is going on, since the units are most likely not attacking in the same order as in the previous session.
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    If you want to use random numbers in your game, then most of the time what you should do is:
    • Create a single random-number generator at the start of your game
    • Initialize it using a seed based on the current time or based on some external true-random source
    • Use that same random-number generator for ALL the random numbers in your whole game (don't make more than one)
    • Never seed it a second time
    Because this is so common, Unity actually does it for you: UnityEngine.Random is an auto-seeded random number generator that is globally visible so that you can use it everywhere that you need a random number. If you just use that with no modifications, you'll find that you get different results each time you run your program. No fancy tricks required on your part.

    If you are trying to do something unusual with random numbers, you'll need to be more clear about what you are trying to accomplish.
     
  7. Descus

    Descus

    Joined:
    May 10, 2019
    Posts:
    3
    Okay, im trying to rephrase it. I wanna make a RTS Ranged Unit with a 85% chance to hit the enemy.
    But i want to recreate the scenario in a Replay system.
    My fear is ,by using a seeded randomness, that my unit will always hit or always miss because of a nonchanging seed.
     
  8. kensarto17

    kensarto17

    Joined:
    Nov 11, 2017
    Posts:
    27
    I'm no expert so take what I am about to say with a grain of salt. But as long as you aren't literally reusing the same seed from the same point you started it at, it will be fine.
    The wording is a bit hard to say concisely but if you are "reusing" the seed it will be identical, but if you "continue using" the seed it should have different results
     
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    If by "replay" you mean that you want it to be random and unpredictable when you're actually playing it, but then you want to save a recording and have the recording always be the same when you're watching it, then the typical way to do that would be to use a "random" seed (e.g. the current clock time) when playing, but save that seed along with the replay data, so that the replay can use the same seed.