Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Alright so this is an in game command and it works perfect but I just can't figure out how to make decimal pc = num * rand / 100 because rand doesn't really define one number.

C#
        internal class GambleCommand : Command
{
    public GambleCommand()
        : base("roll", 3)
    {
    }

    protected override bool Process(Player player, RealmTime time, string[] args)
    {
        try
        {
            if (args.Length == 1)
            {
                int num = int.Parse(args[0]);
                Random rnd = new Random();
                string pl = player.Name;
                string rand = "";
                if (num < 0) rand = rnd.Next(num, 0).ToString();
                else rand = rnd.Next(0, num).ToString();
                decimal pc = num * rand / 100;
                foreach (Client i in player.Manager.Clients.Values)
                    {
                    i.SendPacket(new TextPacket
                    {
                        BubbleTime = 0,
                        Stars = -1,
                        Name = pl,
                        Text = "rolled a " + rand + " out of " + num + ". (" + pc + "%)"
                        });
                    }
                }
            }

        catch
        {
            Random rnd = new Random();
            string pl = player.Name;
            string rand = rnd.Next(0, 100).ToString();
            foreach (Client i in player.Manager.Clients.Values)
            {
                i.SendPacket(new TextPacket
                {
                    BubbleTime = 0,
                    Stars = -1,
                    Name = pl,
                    Text = "rolled a " + rand + " out of 100."
                });
            }
        }
        return true;
    }
}

There isn't to much to explain other then that the command basically picks a number out of 0-num (num is a number that you choose). I want it so that it gives the percent when it says it in game. Please help asap!
Posted
Comments
PIEBALDconsult 2-Nov-15 21:31pm    
0) Do NOT keep instantiating new Randoms; make a private class field to hold the Random object, set it omce, and then just use it.
1) Do NOT use ToString().
2) Do NOT expect num * rand / 100 to return a reasonable value.
3) Why, in Bob's name, is rand a string?!
4) Do NOT use string concatenation when String.Format was designed to do what you want.
5) Do NOT repeat yourself -- get rid of that try/catch and use Int32.TryParse

First, you should try with integer
C#
int rand;
...
rand = rnd.Next(num, 0);

And change the formula
C#
int pc = rand * 100 / NUM;


Should be better !
 
Share this answer
 
Maybe try decimal.Parse or decimal.TryParse, if you want to continue on the road of string conversions.
C#
decimal pc = num * decimal.Parse(rand) / 100;
 
Share this answer
 
Comments
Member 12108243 2-Nov-15 21:29pm    
This works to some extent but then I realized I should change it to
decimal pc = num * decimal.Parse(rand) / num;
But still I have an issue of the percent going up for ex: num = 1000 rand = 500
500% is what comes out... Any idea of how I could fix?

Now that I look at it again it's all messed up XD I don't understand what I did
Alright I fixed it so I had to change it to
C#
decimal pc = decimal.Parse(rand) / num * 100;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900