By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,369 Members | 1,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,369 IT Pros & Developers. It's quick & easy.

VB.NET Random / C++ rnd

P: n/a
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.
Aug 6 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a
"Nick" <a@a.comwrote in message
news:%2****************@TK2MSFTNGP05.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.
Aug 6 '08 #2

P: n/a
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:Ja******************************@earthlink.co m...
"Nick" <a@a.comwrote in message
news:%2****************@TK2MSFTNGP05.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.

Aug 6 '08 #3

P: n/a
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:Ja******************************@earthlink.co m...
"Nick" <a@a.comwrote in message
news:%2****************@TK2MSFTNGP05.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.


Aug 6 '08 #4

P: n/a
Hi Gillard,

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

Nick.

"Gillard" <gillard_georges@@@@@@@@@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.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:Ja******************************@earthlink.co m...
>"Nick" <a@a.comwrote in message
news:%2****************@TK2MSFTNGP05.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.


Aug 6 '08 #5

P: n/a
Does 'Microsoft.VisualBasic.VBMath.Rnd' produce the same results as your C++
rnd() method?

--
Mick Doherty
http://www.dotnetrix.co.uk/nothing.html
"Nick" <a@a.comwrote in message
news:#j**************@TK2MSFTNGP05.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.
Aug 6 '08 #6

P: n/a
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"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]wrote in
message news:u1**************@TK2MSFTNGP05.phx.gbl...
Does 'Microsoft.VisualBasic.VBMath.Rnd' produce the same results as your
C++ rnd() method?

--
Mick Doherty
http://www.dotnetrix.co.uk/nothing.html
"Nick" <a@a.comwrote in message
news:#j**************@TK2MSFTNGP05.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.

Aug 6 '08 #7

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

"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]wrote in
message news:u1**************@TK2MSFTNGP05.phx.gbl...
Does 'Microsoft.VisualBasic.VBMath.Rnd' produce the same results as your
C++ rnd() method?

--
Mick Doherty
http://www.dotnetrix.co.uk/nothing.html
"Nick" <a@a.comwrote in message
news:#j**************@TK2MSFTNGP05.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.

Aug 6 '08 #8

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

"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]wrote in
message news:u1**************@TK2MSFTNGP05.phx.gbl...
Does 'Microsoft.VisualBasic.VBMath.Rnd' produce the same results as your
C++ rnd() method?

--
Mick Doherty
http://www.dotnetrix.co.uk/nothing.html
"Nick" <a@a.comwrote in message
news:#j**************@TK2MSFTNGP05.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.

Aug 6 '08 #9

P: n/a
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",CallingConventio n:=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:
ms****@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx .
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 7 '08 #10

P: n/a
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",CallingConventio n:=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:
ms****@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx .
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 7 '08 #11

P: n/a
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
_____
http://www.guffa.com
Aug 7 '08 #12

P: n/a
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:
ms****@microsoft.com.

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

Aug 8 '08 #13

P: n/a
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-******@online.microsoft.comwrote in message
news:z9****************@TK2MSFTNGHUB02.phx.gbl...
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:
ms****@microsoft.com.

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

Aug 8 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.