问题
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
thks!
回答1:
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];
}
回答2:
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
回答3:
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);
回答4:
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
来源:https://stackoverflow.com/questions/10903014/there-is-any-way-to-match-the-method-randint-of-visual-fox-pro-and-c-net