473,414 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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.
Aug 6 '08 #1
13 3641
"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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
6
by: Acacia | last post by:
How would you generate a random number in C++?
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
10
by: Marshall Belew | last post by:
I'm trying to synchronize a network app that uses random numbers generated by System.Random. Rather than pass every randomly generated number, I just pass the seed. I'm seeing a result that leads...
15
by: Steven Macintyre | last post by:
Hi all, I need to retrieve an integer from within a range ... this works ... below is my out puts ... it just does not seem so random ... Is there perhaps a suggestion out there to create a...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
8
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
6
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.