+ Reply to Thread
Results 1 to 6 of 6

Thread: PRNG in C#, VB, result is different, can someone help me?

  1. #1
    Member BarkingFrog's Avatar
    Join Date
    Apr 2009
    Location
    Somewhere over the rainbow
    Age
    24
    Posts
    28

    PRNG in C#, VB, result is different, can someone help me?

    I'm only using this for personal use, just to calculate things..
    but I need some help.. I use VisualStudio 2005

    first, i used VB
    Code:
    Private Function PRNG(ByVal seed As String) As String
      Dim temp As String = ""
      Dim t As UInt64
      t = Convert.ToUInt64("0x41C64E6D", 16) * Convert.ToUInt64(seed, 16) + Convert.ToUInt64("0x6073", 16)
      temp = Hex(t)
      If temp.Length < 16 Then
        For t = 0 To 15 - temp.Length Step 1 'just to add zero
          temp = "0" & temp
        Next
      End If
      PRNG = temp
    End Function
    then for some reason i have to convert it to C#, here is the code:
    Code:
    using Microsoft.VisualBasic; //for Conversion
    
    private const UInt64 factor = 0x41C64E6D;
    private const UInt64 adder = 0x6073;
    
    //...
    
    private string generate(String seed) {
      return temp =  Conversion.Hex( (factor * Convert.ToUInt64(seed, 16) + adder));
    }
    but i get different result, well the LSB is the same, but the MSB is different.. any clue?

    thx a lot

  2. #2
    Learning to breathe IRC VOPDeveloperGame Save ResearcherModerator codemonkey85's Avatar
    Join Date
    Apr 2009
    Location
    http://goo.gl/Wd4id
    Age
    27
    Posts
    800

    Re: PRNG in C#, VB, result is different, can someone help me?

    I am not sure why the result would be different, but here's what the VB.Net code converted to C# looks like for me (courtesy of this page.):

    Code:
    private string PRNG(string seed)
    {
        string temp = "";
        UInt64 t = default(UInt64);
        t = Convert.ToUInt64("0x41C64E6D", 16) * Convert.ToUInt64(seed, 16) + Convert.ToUInt64("0x6073", 16);
        temp = Conversion.Hex(t);
        if (temp.Length < 16) {
            for (t = 0; t <= 15 - temp.Length; t += 1) {
                //just to add zero
                temp = "0" + temp;
            }
        }
        return temp;
    }

  3. #3
    Making Tools ROM Researcher
    Join Date
    May 2009
    Posts
    6

    Re: PRNG in C#, VB, result is different, can someone help me?

    public UInt32 GetNext32BitNumber()
    {
    seed = seed * 0x41C64E6D + 0x6073;

    return seed;
    }

    No need for all that 64 bit stuff.

  4. #4
    ... DeveloperRAM ResearcherHelpful Q&A MemberFormer Staff damio's Avatar
    Join Date
    Apr 2009
    Location
    Australia.
    Posts
    676

    Re: PRNG in C#, VB, result is different, can someone help me?

    Quote Originally Posted by mingot View Post
    public UInt32 GetNext32BitNumber()
    {
    seed = seed * 0x41C64E6D + 0x6073;

    return seed;
    }

    No need for all that 64 bit stuff.
    Shouldn't it be seed = (seed * 0x41C64E6D + 0x6073) & 0xFFFFFFFF;
    ?

  5. #5
    Making Tools ROM Researcher
    Join Date
    May 2009
    Posts
    6

    Re: PRNG in C#, VB, result is different, can someone help me?

    Nope. The return will be uint anyhow.

  6. #6
    Member BarkingFrog's Avatar
    Join Date
    Apr 2009
    Location
    Somewhere over the rainbow
    Age
    24
    Posts
    28

    Re: PRNG in C#, VB, result is different, can someone help me?

    Thanks for all of the help, I'll try that again..
    oh, for the UInt64, i've tried UInt32, sometimes it says overflow.. so I decided to use UInt64.. but I think Mingot is correct, I'll try again..

    @Damio
    the modulo thing is somehow does not effect the result..

    well I think it's because the conversion, somehow the result from C# is like rounded up..

    thanks all..

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
PPN Top 50