Hey guys, here's my question.

In this text rpg i'm making i'm trying to add a "critical strike" chance, so that the player hits more, i got the hitting more part down right, but my problem is that for example, say the player can hit from 1 to 5 HP, based on the player's stats and also random, since the lowest number is the player's attack, and highest is the player's strength, it looks like this.

Random hit = new Random();
int n = hit.Next(playerattack,playerstrength)

now, with the critical strike calculation, it's completely random from 1 to 100.

my problem is, that both numbers are somehow correlated, which is defenitely not what i want, when the critical strike random number is for example, 90. then the player hits pretty hard, in this case a 4 or 5, but when the critical random is 20, then he might hit 1 or 2.

The problem with this is that say the player's critical chance is 20%, that means that he will get a bonus if he hits 20% or lower of his hit calculation, which is dumb, since the critical strike bonus would then be way too low to matter.

Same thing if i turn it around, every hit that's 20% or above will be a critical strike, which is nice, but it won't affect even take place if the hit is lower than 20%.

public void Force()
        {
            Weaponcalculation();
            Random hit = new Random();
            h = hit.Next(playermaxattack, playermaxstrength);
            Chance();
            Console.WriteLine("chance {0}", c);
            Console.WriteLine("hit {0}", h);
            
            Console.ReadLine();
            if (total > c)
            {
                h = h * 2;
                Console.WriteLine("You've hit a critical blow!");
            }
            else
            {
            }
          
        }
        public void Chance()
        {
            Random ok = new Random();
            c = ok.Next(1, 101);

        }

 total = player's chance to hit a critical strike

My question is.

How do i make two separate Random numbers at the same time?

Recommended Answers

All 11 Replies

Use this instead of the random number class in the .NET framework:

public static int GetRandom(int Maxvalue)
    {
      byte[] randomNumber = new byte[1];
      System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
      Gen.GetBytes(randomNumber);
      int rand = Convert.ToInt32(randomNumber[0]);
      return rand % Maxvalue + 1;
    }

Note in there that i'm only filling a single byte[] so this won't generate large random numbers

commented: nice angle to view random numbers! +8

Thank you for the speedy answer.

But i can't seem to find that Class, i've never touched the .NET framework thing, how do i access it?

Are you using Visual Studio C# 2008?

i am using Microsoft Visual c# 2008 express edition

What class can't you find? According to the help file:

Implements a cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider (CSP). This class cannot be inherited.

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

You should have a reference to that class. It is in the core of the framework.

Oh man, haha i feel so dumb.

I don't know what to do, I went to help and found what you showed me, but i don't know if you want me to edit something with the code you first gave me, if that's the case i don't know how to do it.

When i go to object browser it doesn't let me edit anything.

Gah.

Gah: I'm having trouble following this thread. Based on the original code you submitted, what is the line of code you want to modify and from what?

You can leave out all the "game" details and just tell me the number(s) you need and show me the line(s) please (just the logic in other words).

okay, basically i want a completely different and random range of numbers everytime i make a Random instance.

there's 2 numbers i want randomized, one equals the damage the player lands, and the other is the critical chance number, which goes from 1 to 100.

The problem is, that both "random" numbers are somewhat alike, as in if the critical is 90, then the damage the player lands is very high, if the critical is 50, then damage is mid and so on.

So i want completely different numbers for each, because say if the player's chance to hit a critical is 10%, it would mean the "randomized" critical number would have to be less or equal to ten, which would in turn make the damage the player inflicts really low, therefore a crit wouldn't make a big difference.

I can't post a code since i don't actually know what's wrong, the code i already posted is what's fishy.

LOL... I think you might be asking multiple questions here, so let's start with the first:

okay, basically i want a completely different and random range of numbers everytime i make a Random instance.

Here is an answer:

// Ensure our numbers are indeed random
            // Passing in the Millisecond is known as "seeding"; without seeding, each instance would probably produce the same result
            Random hit1 = new Random(DateTime.Now.Millisecond);
            System.Threading.Thread.Sleep(5);// 5 milliseconds just in case
            Random hit2 = new Random(DateTime.Now.Millisecond);

            // both of these will produce two different random numbers in range 50 - 99
            int num1 = hit1.Next(50, 100);
            int num2 = hit2.Next(50, 100);

Now, is there another question?

Heh.

Yes sir, your answer fixed everything, thank you very much. I hate getting stuck like this for so long but i guess i better get used to it.

No problem--glad it worked for you. Please mark this thread as resolved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.