VB code functions Randomize() and like more in c#

662 views Asked by At

Let me know how to write the this three function in c#: Randomize, Int ,Ran()

Randomize();

    //  This is my VB Code
    for (  ; (KeyOK(sKey) == false); )
    {
        for ( ; (PartOK(sPart2) == false);  )
        {
            sPart2 = "";
            for (lCount = 1; (lCount <= PART_LENGTH); lCount++) {
                sPart2 = (sPart2 +  Int((10 *  Rnd())).ToString());
            }
        }
3

There are 3 answers

0
Mithrandir On

Use the class Random to get a random number.

 Random rnd = new Random();

Then use one of the overloaded method calls of the Next() method to get a random number.

0
Guffa On

That's not VB code, that's some strange mix...

First create a Random object (outside the loop):

Random rnd = new Random();

Use the Next method to get a random number from it:

sPart2 += rnd.Next(0, 10).ToString();
0
phipsgabler On

Additionally, note that most VB built-in functions and constants can be found in the Microsoft.VisualBasic namespace (including MsgBox, which looks kinda strange in C# but I find it practical sometimes), which is accessible by C# .

Now, Random has been explained already. Int can be translated in different ways. Mostly with a conversion to int, but for special behaviour look at Math.Round, Math.Floor and Math.Ceiling.