views:

129

answers:

1

How can I make a script that makes one thing happen 4/5 times and the other thing 1/5 times

I need it to appear after this

private void Arrest()
{
    Ped Criminal = Player.GetTargetedPed();
    if (Exists(Criminal) && 
        (Player.Character.Position.DistanceTo(Criminal.Position) <= 10.0F))
    {
+6  A: 
static Random rnd = new Random();
static void DoOne() { ... }
static void DoTwo() { ... }
static void RollDice() {
   if (rnd.Next(5) == 0)
       DoOne(); // happens 1/5 times
   else
       DoTwo(); // happens 4/5 times
}

It is important you don't recreate Random instance on each call. You should keep the old instance around and reuse it. Otherwise, the randomly generated sequence will not be uniformly distributed.

Mehrdad Afshari
lilmcnessy
@lilmcnessy: So? Put it in there, modify it to do what you want. We're not going to do your work for you, we're here to answer questions for you to help you be a better programmer. In this case, @Mehrdad has thoughtfully provided you with a pattern that will allow you to have one thing happen approximately once every five times, and another thing happen four times of every five times. So I think the appropriate response from you is "thank you", not "put it after my code and make it work for me"
Randolpho
@Mehrdad: even worse, if you re-create the Random instance on each call using the empty constructor, there's a chance you'll get the exact same sequence repeated, since the empty constructor is seeded with a time-based value. There was an SO question a couple months ago where the asker convinced himself that he had miraculously produced duplicate GUIDs, when all he was doing was instantiating two Random instances with the same time-based value.
MusiGenesis
@Randolpho I apologised to Mehrdad for not saying want I want specifically, and I didn't demand "put it after my code and make it work for me", I am not very experienced so I need a little help
lilmcnessy
@lilmcnessy: That's understandable, but if you want the experience necessary, you'll mess around until you get it right. One of the best ways to learn is by doing it yourself.
Randolpho
Does DoOne and DoTwo have to be static?
lilmcnessy
Not necessarily. But if `RollDice` is `static`, you should either make those `static` too or use an object instance to call them; just like any other `static` thing.
Mehrdad Afshari