migrate method Rand(int) Visual Fox Pro to C#.net

百般思念 提交于 2019-12-27 05:35:23

问题


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.

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:


as i posted in your other question http://foxcentral.net/microsoft/vfptoolkitnet.htm the VFP toolkit for .net might have the same rand generator function




回答2:


Your solution could really be as simple as two existing methods in C# .net 4.0

public int MyRandomFunction(string seedString)
{
    int hashCode = seedString.GetHashCode(); // Always returns the same integer based on a string
    Random myGenerator = new Random(hasCode);
    return myGenerator.Next(10000, 99999); // Returns a number between 10000 and 99999, ie 5 digits
}

You will always get the same initial value, because you are always starting with the same seed.




回答3:


The equivalent of

Rand(vcad)

is

(new Random(vcad)).Next();

The equivalent of

x = Rand(seedValue)
y = Rand()

is

Random r = new Random(seedValue);
x = r.Next();
y = r.Next();

However, you should consider yourself extremely lucky if these equivalent statements actually produce the same results in VFP as in c#.Net. The underlying implementations would have to be the same, which would surprise me greatly. If they don't produce the same results, and that is a requirement for you, you're left with the task of finding out what FoxPro's internal implementation of the Rand function is, and duplicating that in c# code.

If the range of values for vcad is constrained within some range, the simplest solution would be to use VFP to generate a table of lookup values and use that in your c# conversion.




回答4:


Encapsulate the VFP RAND call in a COM dll and call it from .net if you really need to get the exact same behaviour as described.

Seems an odd design, but that's legacy systems for you I guess.



来源:https://stackoverflow.com/questions/10905171/migrate-method-randint-visual-fox-pro-to-c-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!