473,549 Members | 3,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3650
"Nick" <a@a.comwrote in message
news:%2******** ********@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.
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******** *************** *******@earthli nk.com...
"Nick" <a@a.comwrote in message
news:%2******** ********@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.

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******** *************** *******@earthli nk.com...
"Nick" <a@a.comwrote in message
news:%2******** ********@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.


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_george s@@@@@@@@@hotma il.comwrote in message
news:%2******** ********@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:Ja******** *************** *******@earthli nk.com...
>"Nick" <a@a.comwrote in message
news:%2******* *********@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.


Aug 6 '08 #5
Does 'Microsoft.Visu alBasic.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******** ******@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.
Aug 6 '08 #6
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"
<EX***********@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]wrote in
message news:u1******** ******@TK2MSFTN GP05.phx.gbl...
Does 'Microsoft.Visu alBasic.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******** ******@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.

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.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]wrote in
message news:u1******** ******@TK2MSFTN GP05.phx.gbl...
Does 'Microsoft.Visu alBasic.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******** ******@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.

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.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]wrote in
message news:u1******** ******@TK2MSFTN GP05.phx.gbl...
Does 'Microsoft.Visu alBasic.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******** ******@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.

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:="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:
ms****@microsof t.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
3656
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 appreciated.
6
20697
by: Acacia | last post by:
How would you generate a random number in C++?
10
2487
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 spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone...
10
5955
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 System.Random iRnd = new System.Random(); string theNum = iRnd.Next(0,8).ToString(); lblAnswer.Text = iRnd.Next(0,8).ToString();
10
2490
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 me to believe that a seeded random number is still slightly random. I need a predictable random number. Here's my results Machine 1 Seed:...
15
2946
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 more random int ...? >>> random.randint(3, 8) 7 >>> random.randint(3, 8)
4
2739
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. It should put a blue and yellow box on the page with "This is a test" as part of the picture. But what I get is a broken Gif. The other problem is...
1
338
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N? ----------------------------------------------------------------------- function Random(x) { return Math.floor(x*Math.random()) } gives a random integer in the range from 0 to x-1 inclusive; use « Random(N)+1...
8
3899
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 Random((int)_seedTimer.ElapsedTicks); _random = new Random(randomSeed.Next(0,99999999)); return random.Next(min, max);
6
4801
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
7446
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7718
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6041
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.