VB.NET Random / C++ rnd

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick

    VB.NET Random / C++ rnd

    Hi there,

    I'm trying to create a random number via a seed in VB.NET and C++, 2
    different applications. I want to be able to use the same seed and get the
    same random number but unfortunately VB.NET's Random class produces a
    different set of results than the C++ rnd function.

    I can't see any reference of rnd being used via API declarations and am just
    trying to find a way of generating a random number via seed that is
    identical in both languages. Of course I could create a C++ DLL to do it
    for me and use it in the VB app, but i'd rather not go to that extreme for 1
    method.

    Any suggestions would be greatly appreciated!

    Nick.


  • Michael D. Ober

    #2
    Re: VB.NET Random / C++ rnd

    "Nick" <a@a.comwrote in message
    news:%23ju3Sp79 IHA.3884@TK2MSF TNGP05.phx.gbl. ..
    Hi there,
    >
    I'm trying to create a random number via a seed in VB.NET and C++, 2
    different applications. I want to be able to use the same seed and get
    the same random number but unfortunately VB.NET's Random class produces a
    different set of results than the C++ rnd function.
    >
    I can't see any reference of rnd being used via API declarations and am
    just trying to find a way of generating a random number via seed that is
    identical in both languages. Of course I could create a C++ DLL to do it
    for me and use it in the VB app, but i'd rather not go to that extreme for
    1 method.
    >
    Any suggestions would be greatly appreciated!
    >
    Nick.
    If you are using managed C++ (C++/CLI), you can use the Random class in both
    VB.Net and C++.

    Mike.


    Comment

    • Gillard

      #3
      Re: VB.NET Random / C++ rnd

      be happy to have a different set of result :

      if not why would it be called random ?

      "Michael D. Ober" <obermd.@.alum. mit.edu.nospam. wrote in message
      news:JaWdnSzVr7 bFDATVnZ2dnUVZ_ uLinZ2d@earthli nk.com...
      "Nick" <a@a.comwrote in message
      news:%23ju3Sp79 IHA.3884@TK2MSF TNGP05.phx.gbl. ..
      >Hi there,
      >>
      >I'm trying to create a random number via a seed in VB.NET and C++, 2
      >different applications. I want to be able to use the same seed and get
      >the same random number but unfortunately VB.NET's Random class produces a
      >different set of results than the C++ rnd function.
      >>
      >I can't see any reference of rnd being used via API declarations and am
      >just trying to find a way of generating a random number via seed that is
      >identical in both languages. Of course I could create a C++ DLL to do it
      >for me and use it in the VB app, but i'd rather not go to that extreme
      >for 1 method.
      >>
      >Any suggestions would be greatly appreciated!
      >>
      >Nick.
      If you are using managed C++ (C++/CLI), you can use the Random class in
      both VB.Net and C++.
      >
      Mike.
      >
      >

      Comment

      • Nick

        #4
        Re: VB.NET Random / C++ rnd

        Hi Mike,

        It's not unfortunately, it's C++ / WTL, no managed code in sight
        otherwise I wouldn't have this issue in the first place.

        Nick.

        "Michael D. Ober" <obermd.@.alum. mit.edu.nospam. wrote in message
        news:JaWdnSzVr7 bFDATVnZ2dnUVZ_ uLinZ2d@earthli nk.com...
        "Nick" <a@a.comwrote in message
        news:%23ju3Sp79 IHA.3884@TK2MSF TNGP05.phx.gbl. ..
        >Hi there,
        >>
        >I'm trying to create a random number via a seed in VB.NET and C++, 2
        >different applications. I want to be able to use the same seed and get
        >the same random number but unfortunately VB.NET's Random class produces a
        >different set of results than the C++ rnd function.
        >>
        >I can't see any reference of rnd being used via API declarations and am
        >just trying to find a way of generating a random number via seed that is
        >identical in both languages. Of course I could create a C++ DLL to do it
        >for me and use it in the VB app, but i'd rather not go to that extreme
        >for 1 method.
        >>
        >Any suggestions would be greatly appreciated!
        >>
        >Nick.
        If you are using managed C++ (C++/CLI), you can use the Random class in
        both VB.Net and C++.
        >
        Mike.
        >
        >

        Comment

        • Nick

          #5
          Re: VB.NET Random / C++ rnd

          Hi Gillard,

          LOL! Unfortunately that's not quite how it works, otherwise there would
          be no point in a random seed.

          Nick.

          "Gillard" <gillard_george s@@@@@@@@@hotma il.comwrote in message
          news:%23yolsx79 IHA.5404@TK2MSF TNGP04.phx.gbl. ..
          be happy to have a different set of result :
          >
          if not why would it be called random ?
          >
          "Michael D. Ober" <obermd.@.alum. mit.edu.nospam. wrote in message
          news:JaWdnSzVr7 bFDATVnZ2dnUVZ_ uLinZ2d@earthli nk.com...
          >"Nick" <a@a.comwrote in message
          >news:%23ju3Sp7 9IHA.3884@TK2MS FTNGP05.phx.gbl ...
          >>Hi there,
          >>>
          >>I'm trying to create a random number via a seed in VB.NET and C++, 2
          >>different applications. I want to be able to use the same seed and get
          >>the same random number but unfortunately VB.NET's Random class produces
          >>a different set of results than the C++ rnd function.
          >>>
          >>I can't see any reference of rnd being used via API declarations and am
          >>just trying to find a way of generating a random number via seed that is
          >>identical in both languages. Of course I could create a C++ DLL to do
          >>it for me and use it in the VB app, but i'd rather not go to that
          >>extreme for 1 method.
          >>>
          >>Any suggestions would be greatly appreciated!
          >>>
          >>Nick.
          >If you are using managed C++ (C++/CLI), you can use the Random class in
          >both VB.Net and C++.
          >>
          >Mike.
          >>
          >>

          Comment

          • Mick Doherty

            #6
            Re: VB.NET Random / C++ rnd

            Does 'Microsoft.Visu alBasic.VBMath. Rnd' produce the same results as your C++
            rnd() method?

            --
            Mick Doherty
            Dotnetrix offers Nothing. Standard and Premium versions available.



            "Nick" <a@a.comwrote in message
            news:#ju3Sp79IH A.3884@TK2MSFTN GP05.phx.gbl...
            Hi there,
            >
            I'm trying to create a random number via a seed in VB.NET and C++, 2
            different applications. I want to be able to use the same seed and get
            the same random number but unfortunately VB.NET's Random class produces a
            different set of results than the C++ rnd function.
            >
            I can't see any reference of rnd being used via API declarations and am
            just trying to find a way of generating a random number via seed that is
            identical in both languages. Of course I could create a C++ DLL to do it
            for me and use it in the VB app, but i'd rather not go to that extreme for
            1 method.
            >
            Any suggestions would be greatly appreciated!
            >
            Nick.
            >

            Comment

            • Nick

              #7
              Re: VB.NET Random / C++ rnd

              Hi Mick,

              Unfortunately not,

              Microsoft.Visua lBasic.VBMath.R andomize(871)
              Dim pIntAsciiCode As Integer
              pIntAsciiCode = Rnd()

              Result = 1, presumably due to rounding.

              ...

              srand(871);
              int pIntAsciiCode = 0;
              pIntAsciiCode = rand();

              Result = 2882

              ...

              Hmm this isn't looking good really, a whole DLL to expose 1 method! :(
              :( :(

              Nick.


              "Mick Doherty"
              <EXCHANGE#WITH@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]wrote in
              message news:u1BuNq89IH A.1192@TK2MSFTN GP05.phx.gbl...
              Does 'Microsoft.Visu alBasic.VBMath. Rnd' produce the same results as your
              C++ rnd() method?
              >
              --
              Mick Doherty
              Dotnetrix offers Nothing. Standard and Premium versions available.

              >
              >
              "Nick" <a@a.comwrote in message
              news:#ju3Sp79IH A.3884@TK2MSFTN GP05.phx.gbl...
              >Hi there,
              >>
              >I'm trying to create a random number via a seed in VB.NET and C++, 2
              >different applications. I want to be able to use the same seed and get
              >the same random number but unfortunately VB.NET's Random class produces a
              >different set of results than the C++ rnd function.
              >>
              >I can't see any reference of rnd being used via API declarations and am
              >just trying to find a way of generating a random number via seed that is
              >identical in both languages. Of course I could create a C++ DLL to do it
              >for me and use it in the VB app, but i'd rather not go to that extreme
              >for 1 method.
              >>
              >Any suggestions would be greatly appreciated!
              >>
              >Nick.
              >>
              >

              Comment

              • Nick

                #8
                Re: VB.NET Random / C++ rnd

                I'm just looking at the Rnd method in Reflector and very different from the
                C version unfortunately :(

                "Mick Doherty"
                <EXCHANGE#WITH@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]wrote in
                message news:u1BuNq89IH A.1192@TK2MSFTN GP05.phx.gbl...
                Does 'Microsoft.Visu alBasic.VBMath. Rnd' produce the same results as your
                C++ rnd() method?
                >
                --
                Mick Doherty
                Dotnetrix offers Nothing. Standard and Premium versions available.

                >
                >
                "Nick" <a@a.comwrote in message
                news:#ju3Sp79IH A.3884@TK2MSFTN GP05.phx.gbl...
                >Hi there,
                >>
                >I'm trying to create a random number via a seed in VB.NET and C++, 2
                >different applications. I want to be able to use the same seed and get
                >the same random number but unfortunately VB.NET's Random class produces a
                >different set of results than the C++ rnd function.
                >>
                >I can't see any reference of rnd being used via API declarations and am
                >just trying to find a way of generating a random number via seed that is
                >identical in both languages. Of course I could create a C++ DLL to do it
                >for me and use it in the VB app, but i'd rather not go to that extreme
                >for 1 method.
                >>
                >Any suggestions would be greatly appreciated!
                >>
                >Nick.
                >>
                >

                Comment

                • Nick

                  #9
                  Re: VB.NET Random / C++ rnd

                  right, I've cracked and I now have a managed C++ class library which exposes
                  the C runtime methods.

                  "Mick Doherty"
                  <EXCHANGE#WITH@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]wrote in
                  message news:u1BuNq89IH A.1192@TK2MSFTN GP05.phx.gbl...
                  Does 'Microsoft.Visu alBasic.VBMath. Rnd' produce the same results as your
                  C++ rnd() method?
                  >
                  --
                  Mick Doherty
                  Dotnetrix offers Nothing. Standard and Premium versions available.

                  >
                  >
                  "Nick" <a@a.comwrote in message
                  news:#ju3Sp79IH A.3884@TK2MSFTN GP05.phx.gbl...
                  >Hi there,
                  >>
                  >I'm trying to create a random number via a seed in VB.NET and C++, 2
                  >different applications. I want to be able to use the same seed and get
                  >the same random number but unfortunately VB.NET's Random class produces a
                  >different set of results than the C++ rnd function.
                  >>
                  >I can't see any reference of rnd being used via API declarations and am
                  >just trying to find a way of generating a random number via seed that is
                  >identical in both languages. Of course I could create a C++ DLL to do it
                  >for me and use it in the VB app, but i'd rather not go to that extreme
                  >for 1 method.
                  >>
                  >Any suggestions would be greatly appreciated!
                  >>
                  >Nick.
                  >>
                  >

                  Comment

                  • Feng Chen[MSFT]

                    #10
                    Re: VB.NET Random / C++ rnd

                    Hello Nick,

                    Yes, use managed C++ can wrap the native functions in a library and use it
                    in .net applications. In addition, we can also import and directly call the
                    C runtime functions in the VB.net application using P/Invoke:

                    <DllImport
                    ("msvcr80.dll", EntryPoint:="ra nd",CallingConv ention:=Calling Convention.Cdec l
                    )>
                    Public Shared Function rand() As Integer
                    End Function
                    <DllImport("msv cr80.dll", EntryPoint:="sr and", CallingConventi on:=
                    CallingConventi on.Cdecl)>
                    Public Shared Sub srand(ByVal _Seed As UInteger)
                    End Sub

                    Please let me know if you have any other concerns, or need anything else.

                    Best regards,
                    Feng Chen
                    Microsoft Online Community Support

                    Delighting our customers is our #1 priority. We welcome your comments and
                    suggestions about how we can improve the support we provide to you. Please
                    feel free to let my manager know what you think of the level of service
                    provided. You can send feedback directly to my manager at:
                    msdnmg@microsof t.com.
                    =============== =============== =============== =====
                    Get notification to my posts through email? Please refer to
                    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

                    ications .

                    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
                    where an initial response from the community or a Microsoft Support
                    Engineer within 1 business day is acceptable. Please note that each follow
                    up response may take approximately 2 business days as the support
                    professional working with you may need further investigation to reach the
                    most efficient resolution. The offering is not appropriate for situations
                    that require urgent, real-time or phone-based interactions or complex
                    project analysis and dump analysis issues. Issues of this nature are best
                    handled working with a dedicated Microsoft Support Engineer by contacting
                    Microsoft Customer Support Services (CSS) at
                    http://msdn.microsoft.com/subscripti...t/default.aspx .
                    =============== =============== =============== =====
                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    Comment

                    • Feng Chen[MSFT]

                      #11
                      Re: VB.NET Random / C++ rnd

                      Hello Nick,

                      Yes, use managed C++ can wrap the native functions in a library and use it
                      in .net applications. In addition, we can also import and directly call the
                      C runtime functions in the VB.net application using P/Invoke:

                      <DllImport
                      ("msvcr80.dll", EntryPoint:="ra nd",CallingConv ention:=Calling Convention.Cdec l
                      )>
                      Public Shared Function rand() As Integer
                      End Function
                      <DllImport("msv cr80.dll", EntryPoint:="sr and", CallingConventi on:=
                      CallingConventi on.Cdecl)>
                      Public Shared Sub srand(ByVal _Seed As UInteger)
                      End Sub

                      Please let me know if you have any other concerns, or need anything else.

                      Best regards,
                      Feng Chen
                      Microsoft Online Community Support

                      Delighting our customers is our #1 priority. We welcome your comments and
                      suggestions about how we can improve the support we provide to you. Please
                      feel free to let my manager know what you think of the level of service
                      provided. You can send feedback directly to my manager at:
                      msdnmg@microsof t.com.
                      =============== =============== =============== =====
                      Get notification to my posts through email? Please refer to
                      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

                      ications .

                      Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
                      where an initial response from the community or a Microsoft Support
                      Engineer within 1 business day is acceptable. Please note that each follow
                      up response may take approximately 2 business days as the support
                      professional working with you may need further investigation to reach the
                      most efficient resolution. The offering is not appropriate for situations
                      that require urgent, real-time or phone-based interactions or complex
                      project analysis and dump analysis issues. Issues of this nature are best
                      handled working with a dedicated Microsoft Support Engineer by contacting
                      Microsoft Customer Support Services (CSS) at
                      http://msdn.microsoft.com/subscripti...t/default.aspx .
                      =============== =============== =============== =====
                      This posting is provided "AS IS" with no warranties, and confers no rights.

                      Comment

                      • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                        #12
                        Re: VB.NET Random / C++ rnd

                        Nick wrote:
                        Hi there,
                        >
                        I'm trying to create a random number via a seed in VB.NET and C++, 2
                        different applications. I want to be able to use the same seed and get the
                        same random number but unfortunately VB.NET's Random class produces a
                        different set of results than the C++ rnd function.
                        >
                        I can't see any reference of rnd being used via API declarations and am just
                        trying to find a way of generating a random number via seed that is
                        identical in both languages. Of course I could create a C++ DLL to do it
                        for me and use it in the VB app, but i'd rather not go to that extreme for 1
                        method.
                        >
                        Any suggestions would be greatly appreciated!
                        >
                        Nick.
                        >
                        >
                        You can easily roll your own random method that is reproducable in most
                        any language. Here is one in C# (excuse me for posting in vb) for
                        creating a byte random number:


                        private byte _seed = 1;

                        public byte Rnd() {
                        _seed = byte((int)_seed * 251 + 1);
                        return _seed;
                        }


                        The algorithm is simple: multiply the seed with a large enough prime
                        number, add one, and throw away any overflow bits.

                        For data types larger than a byte you would use a larger prime number,
                        something close to it's max value.

                        I picked up this algorithm many years ago from the random function in
                        Turbo Pascal. There are more advanced algorithms that gives a bit better
                        randomness, but I thought that you would want simplicity rather than
                        extreme randomness.

                        --
                        Göran Andersson
                        _____
                        Göran Anderssons privata hemsida.

                        Comment

                        • Feng Chen[MSFT]

                          #13
                          Re: VB.NET Random / C++ rnd

                          Hello Nick,

                          I haven't seen your reply after I last posted my reply, I'm writing to
                          check the status of this post. Please feel free to let me know if there's
                          anything else I can help.

                          Best regards,
                          Feng Chen
                          Microsoft Online Community Support
                          =============== =============== ===========
                          Delighting our customers is our #1 priority. We welcome your comments and
                          suggestions about how we can improve the support we provide to you. Please
                          feel free to let my manager know what you think of the level of service
                          provided. You can send feedback directly to my manager at:
                          msdnmg@microsof t.com.

                          This posting is provided "AS IS" with no warranties, and confers no rights.

                          Comment

                          • Nick

                            #14
                            Re: VB.NET Random / C++ rnd

                            Hi Fenh,

                            Tis okay I said that I had sorted the problem, although I'm not using
                            the method via DLL Imports, I've wrapped the methods so that they are more
                            like the native .NET Random class but produce the same results as the C
                            Runtime calls.

                            Nick.

                            ""Feng Chen[MSFT]"" <v-fengch@online.m icrosoft.comwro te in message
                            news:z9mwlUT%23 IHA.5076@TK2MSF TNGHUB02.phx.gb l...
                            Hello Nick,
                            >
                            I haven't seen your reply after I last posted my reply, I'm writing to
                            check the status of this post. Please feel free to let me know if there's
                            anything else I can help.
                            >
                            Best regards,
                            Feng Chen
                            Microsoft Online Community Support
                            =============== =============== ===========
                            Delighting our customers is our #1 priority. We welcome your comments and
                            suggestions about how we can improve the support we provide to you. Please
                            feel free to let my manager know what you think of the level of service
                            provided. You can send feedback directly to my manager at:
                            msdnmg@microsof t.com.
                            >
                            This posting is provided "AS IS" with no warranties, and confers no
                            rights.
                            >

                            Comment

                            Working...