2

I'm migrating a Visual Fox Pro code to C #. NET

What makes the Visual Fox Pro: generates a string of 5 digits ("48963") based on a text string (captured in a textbox), if you always enter the same text string will get that string always 5 digits (no reverse), my code in C #. NET should generate the same string.

There is some code that I can not play in dot.net Rand (int)

in VisualFoxPro:

rand(intValue)

in C #. net:

Random r = new Random ();
return r.Next(intValue);

in C# I can´t generate a single value based on the same intValue I know they are very different libraries (VFP and C #) but not if there is any way to match the method of Visual Fox Pro and C #. Net

I want to migrate the following code (Visual Fox Pro 6 to C#)

gnLower = 1000
gnUpper = 100000
vcad = 1
For y=gnLower to gnUpper step 52
    genClave = **Rand(vcad)** * y
    vRound = allt(str(int(genclave)))
    IF Len(vRound) = 3
            vDec = Right(allt(str(genClave,10,2)), 2)
            finClave = vRound+vDec
            Thisform.txtPass.value = Rand(971);
    Exit
    Endif
Next y

outputs:

vcad = 1 return: 99905 vcad = 2 return: 10077 vcad = thanks return: 17200
TylerH
  • 20,799
  • 66
  • 75
  • 101
ch2o
  • 815
  • 2
  • 12
  • 29

4 Answers4

4

Rand in .NET is not guaranteed to be the same between major revision numbers, so a Rand() with a seed of 1234 in 2.0 can be different than a Rand() in 4.0 with the exact same seed.

If you MUST match the old implientation you will need to find out how Visual Fox Pro did their Rand function. However, if you want same behavior, but not the same numbers you can hash the string just output that.

Random r = new Random (myTextBox.Text.GetHashCode()); 
return r.Next();

Now this is not cryptographically secure and is not guaranteed to generate the same number on different computers (it returns different numbers between 32 and 64 bit, and different versions based on the .Net run-time (This actually applies to both GetHashCode and Random itself!)), so don't store it a database!


If you need the same number out every time from the same string in no matter what computer it is on just use a RNGCryptoServiceProvider in the System.Security.Cryptography namespace.

//Returns the same number between 0 and 255 every time.
using(var myRng = new RNGCryptoServiceProvider(myTextBox.Text))
{
    var ret = new byte[1];
    myRng.GetBytes(ret);
    return ret[0];
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
3
Random r = new Random (intValue); 
return r.Next();

See the constructor for Random() :

Providing an identical seed value to different Random objects causes each instance to produce identical sequences of random numbers

rene
  • 41,474
  • 78
  • 114
  • 152
  • thanks, now I can generate the same number Now I need get the same result in both methods (FoxPro and C #. net) – ch2o Jun 05 '12 at 19:39
  • I would start a new question because that is a different beast and I expect not an easy answer. – rene Jun 05 '12 at 20:33
2

In Visual FoxPro, you can generate the same sequence of random numbers repeatedly by calling RAND() once with a seed value, then omitting the seed on subsequent calls:

RAND(mySeed)
RAND()
RAND()

In C# you can do something similar by specifying a seed value as an argument to the Random constructor:

Random r = new Random (mySeed);  
r.Next(intValue); 
r.Next(intValue); 
Joe
  • 122,218
  • 32
  • 205
  • 338
1

I used the GetHashCode method on the string value to seed Random:

var s = "abcdefg";
var random = new Random(s.GetHashCode());
var hash = random.Next(10000, 99999));

Here are the results I got with a few test cases:

"abcdefg" => 43065
"abcdefg" => 43065
"defghij" => 62962
"qwerty"  => 72764
"defghij" => 62962
"qwerty"  => 72764
"abcdefg" => 43065
FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
  • I need get the same result in both methods (FoxPro and C #. net) – ch2o Jun 05 '12 at 19:40
  • To get the same result in both FoxPro and C#, I imagine that you'd need to role you own versions of the `GetHashCode` method and the `Random` class. That way, you could implement the same algorithm in both. Or maybe, move the code to a service and call that from FoxPro and C# respectively. That way you'd know you'd always get the same number, because it would be the same code. – FishBasketGordo Jun 05 '12 at 21:49