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. Dismiss Notice

Better way to calculate chance?

Discussion in 'Scripting' started by Hydropulse17, Apr 13, 2016.

  1. Hydropulse17

    Hydropulse17

    Joined:
    Jan 26, 2014
    Posts:
    22
    So I've been calculating chance by generating a random number between one and ten, and then checking to see if the number is greater than 9, and if it is, execute some code. That way there is a 10% chance that the code will be executed. Or I could check if it is greater than or equal to 5, for a 50% chance, etc.

    It works just fine, but I figure there ought to be a better way, perhaps a single line of code like "Do this 20% of the time{ code };

    I'm using this for many things, for example, the likelihood that a particular power up will appear upon the destruction of the gameObject.
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
  3. vikankraft

    vikankraft

    Joined:
    Feb 25, 2016
    Posts:
    88
    Well one way could be to check time or something. if get.seconds % 10 == 0. etc (if you just want other ways to do it)
    The problem with all these solutions is ofcource that computers cannot (really) generate random numbers.
     
  4. Hydropulse17

    Hydropulse17

    Joined:
    Jan 26, 2014
    Posts:
    22
    That is much cleaner, thanks for that. In fact, the example on that script reference gave me an idea, so double thanks. I'll probably take the time to change my scripts sometime, but I'll use it in the future for sure.


    Yes, well personally, I don't believe even the universe is truly random. Everything can be determined to have been the result of something else, calculably, if we had the technology. But that's just my deranged philosophy.
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Quantum Physics seems to indicate that "randomness" does exist. Ie: We cannot predict the outcome of quantum interactions before we measure them. Particles behave as average of all possible outcomes before we measure, which collapses down to a specific outcome when we measure. We don't know why a specific outcome is picked. You start getting into hypothesis about parallel and holographic universes where other outcomes to the quantum interactions are picked.
    http://www.askamathematician.com/2009/12/q-do-physicists-really-believe-in-true-randomness/

    tl;dr: Modern physics says there are outcomes we cannot predict. And the universe is weird. =p
     
    MD_Reptile likes this.
  6. vikankraft

    vikankraft

    Joined:
    Feb 25, 2016
    Posts:
    88
    All I meant is that the "random numbers" generated from a computer is not random and can be calculated. You can predict what the most likely outcome is in physics but that dont mean you know whats gonna happen :)
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    Of course it "can" be calculated, since the computer IS calculating it.

    Thing is, with the complexity of most modern random number generators, no human is going to calculate it on a whim. Hell... guessing the 'current second' is easier to force then something from a 32-bit RNG.

    Sure, back in the 8-bit era, and even 16-bit era, when your RNG had a much shorter range (0-255 is pretty short)... you could predict the next value in a RNG twister.

    Anyways... psuedo-RNG generators like those in computers are supposed to have a very special property to it. They have a linear distribution across the entire set of values. And this can be trusted upon. Also since they're calculated, that means they're deterministic, allowing for reusable simulation.

    What's so good about a linear distribution? This means you can apply custom distribution curves to it to get results like you want by a merely multiplying (a normal curve distribution for instance).


    I mean heck, what do you need "true" random for? It's a video game...

    If you can explain the difference between calculated randomness, and "true" randomness, and how that difference can make my game better... maybe I'll consider the importance.
     
  8. Hydropulse17

    Hydropulse17

    Joined:
    Jan 26, 2014
    Posts:
    22
    I know what modern science thinks, but modern science is full of laws and doubt concerning what is achievable through science and technology. I believe there will come a day, be it eons from now, that technology will dictate the laws of physics and not the other way around. I think science, the practice of becoming less ignorant, is pretty ignorant itself, with every generation of scientists calling something impossible or crazy, for the next to prove them wrong. Perpetual motion, creation of energy without decaying matter, creation of mass without pair production, etc.

    I think it was Einstein, I'm not sure, that compared the entirety of man's knowledge to a light on the wall in a dark room. As we come to understand our universe, the light gets bigger, but we become aware of more darkness, more that we don't know. But we can only imagine how much darkness there is, we can only imagine the bounds of this room. I'm not a scientist, I'm a dish washer and an aspiring game developer that wasted a lot of time on wikipedia when I was supposed to be doing my work in high school, so I don't claim to KNOW anything, I just believe in science religiously, and thus I believe that we will be able to predict everything eventually. Even things currently understood to be truly random.

    Edit: A quick Google search revealed that it was in fact Einstein.


    “As our circle of knowledge expands, so does the circumference of darkness surrounding it.” ~Albert Einstein