VB.NET Random / C++ rnd

N

Nick

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.
 
G

Gillard

be happy to have a different set of result :

if not why would it be called random ?
 
N

Nick

Hi Gillard,

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

Nick.
 
N

Nick

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.
 
M

Mick Doherty

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

Nick

Hi Mick,

Unfortunately not,

Microsoft.VisualBasic.VBMath.Randomize(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"
 
N

Nick

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

"Mick Doherty"
 
N

Nick

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

"Mick Doherty"
 
F

Feng Chen[MSFT]

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:="rand",CallingConvention:=CallingConvention.Cdecl
)>
Public Shared Function rand() As Integer
End Function
<DllImport("msvcr80.dll", EntryPoint:="srand", CallingConvention:=
CallingConvention.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:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
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/subscriptions/support/default.aspx .
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
F

Feng Chen[MSFT]

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:="rand",CallingConvention:=CallingConvention.Cdecl
)>
Public Shared Function rand() As Integer
End Function
<DllImport("msvcr80.dll", EntryPoint:="srand", CallingConvention:=
CallingConvention.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:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
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/subscriptions/support/default.aspx .
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Göran Andersson

Nick said:
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.
 
F

Feng Chen[MSFT]

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:
(e-mail address removed).

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

Nick

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top