-4

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!

ch2o
  • 815
  • 2
  • 12
  • 29
  • I need get the same result in both methods Rand (FoxPro and C #. net) – ch2o Jun 05 '12 at 21:28
  • At least sketch your problem and show what you tried. Stackoverflow isn't a place full of golden leprechauns offering a code convert service. Try out a code converter. – Kim Jun 05 '12 at 21:29
  • 1
    Look at the [`Random`](http://msdn.microsoft.com/en-us/library/system.random.aspx) class -- does that have any applicable "Rand(int)" methods? (I have no idea that the "Rand(int)" semantics in VFP are.) –  Jun 05 '12 at 21:36
  • virtual repost of http://stackoverflow.com/questions/10903014/there-is-any-way-to-match-the-method-randint-of-visual-fox-pro-and-c-net – hatchet - done with SOverflow Jun 05 '12 at 22:13
  • Self-duplicate of [there is any way to match the method RAND(INT) of Visual Fox Pro and C #. Net](https://stackoverflow.com/questions/10903014/there-is-any-way-to-match-the-method-randint-of-visual-fox-pro-and-c-net) – TylerH Mar 26 '20 at 15:21

4 Answers4

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

Jay
  • 303
  • 1
  • 3
  • The toolkit does have a Rand function defined. It just calls the normal c# Random.Next function. It seems like a design flaw (in the user's code) to have relied on any API's implementation of random number generation to return identical sequences in all future versions. – hatchet - done with SOverflow Jun 05 '12 at 22:32
  • as mentioned by hatchet Rand function (Visual Fox Pro Toolkit. NET) defined: public static double Random (int nSeed) { R = new System.Random System.Random (nSeed); r.Next return (); } if nSeed = 971 In VFP 6 this method returns 0.70 In C # this method returns 2627119 dealing with the method Random.NextDouble (nSeed) returns 0.0012234761202029 I Convens you can not reproduce this method or importing libraries to. net My last resort would be to use an application that accepts parameters, but do not do this in Visual Fox Pro 6, so here I leave this research, thanks – ch2o Jun 06 '12 at 15:01
0

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.

  • the Random method in C # and VFP is not equivalent and require an application made ​​in VFP so we can get the same result. – ch2o Jun 06 '12 at 15:04
0

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.

EtherDragon
  • 2,679
  • 1
  • 18
  • 24
0

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.

Swordblaster
  • 356
  • 2
  • 7