.NETGURU
Random numbers in a User Control not so random
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngreuse' list.


Thomas Stone
Hi,

Problem: I am writing some tutorials and am trying to demonstrate a simple
example of a random-quote generator, provided in a User Control, and then
used twice (or more) on an .aspx page. I want to show that the two
instances of the user control on the page are independent of each other --
that the output dynamically created by one is separate from that produced
by the other(s).

I can get it to work only by resorting to old VB6 style methods, not by
using the Random class from the .NET Framework. By refreshing enough times,
the quotes are shown to be independent of each other. But when using the
Random class instead, the quotes are NEVER different -- they are different
across refreshes, but the two instances on the page are always identical.

Here are the beginnings of my function (located in a code-behind file for
the user control) in C# and then in VB.NET, attempting to use the Random
class from the .NET Framework:

public string RandomQuote()
{
int intRandomNum;
Random rndQuote = new System.Random();
intRandomNum = rndQuote.Next(1,4);

Public Function RandomQuote() As String
Dim intRandomNum as Integer
Dim rndQuote As Random = New System.Random()
intRandomNum = rndQuote.Next(1,4)

But these attempts don't work: I get the same quote in both instances on
the page each time I refresh (if one is quote A, then the other is Quote A,
and so on). It also doesn't matter if I used NextDouble() instead of Next
(). In the case where I need a random number from 1, 2, or 3, I can do
this:

intRandomNum = (2) * rndQuote.NextDouble() + 1

But that didn't make any difference either.

What *did work* -- in VB.NET -- was to rely on the old carryover methods
from VB6:

Public Function RandomQuote() As String
Randomize
Dim intRandomNum as Integer
intRandomNum = (2) * Rnd + 1

This worked fine. Presumably because the "randomize" keyword does the
seeding work I need?

Now, the SDK suggests that to properly seed the Random class, you can
provide it a time variable in the Random(int) constructor. This is the kind
of code they suggest (I think):

public string RandomQuote()
{
int intRandomNum;
Random rndQuote = new System.Random(unchecked((int)DateTime.Now.Ticks));
intRandomNum = rndQuote.Next(1,4);

But this didn't help me either. I'm running this on my laptop (700 MHz)
with not much else taking up cycle times and such. So presumably it works
faster than the Ticks property can be changed? I also tried the Millisecond
property instead of Ticks, but that didn't work either.

SO... how can I get two different random numbers like this when I am using
the same class twice on a page (via using a user control twice on that
page)? I can do it using the old VB6-style methods, but I can't figure out
how to do it in VB.NET and C# using proper .NET Framework classes. And that
is what I need -- since these are .NET Framework tutorials I am working on
afterall!

Thanks, as always, for any assistance you can provide!

Best,

Tom S.

Reply to this message...
 
    
Andy Smith
well, lesse...

you could use the UserControl's GetHashCode method to help differentiate =
the seed amoung the various instances.

Try using a combination of the Tick and the hashcode. that should =
provide a nicely different random number for the UserControl.

__
Andy Smith
Keyboard Jockey #3a7-2.78.1

[Original message clipped]

Reply to this message...
 
    
Paul Smith
Hi,
The Random method is commonly seeded with a DateTime instance.

Random rndQuote = new Random((int)DateTime.Now.Ticks);

----- Original Message -----
From: "Thomas Stone" <Click here to reveal e-mail address>
To: "aspngreuse" <Click here to reveal e-mail address>
Sent: Tuesday, July 16, 2002 5:39 AM
Subject: [aspngreuse] Random numbers in a User Control not so random

[Original message clipped]

Reply to this message...
 
    
Ryan Trudelle-Schwarz
Or you can always write a good random number generator ;)
I converted the source of a very common c implementation a few months
back and it is fantastic.

Also, iirc, someone mentioned the System.Cryptography name space had a
decent RNG...

-> -----Original Message-----
-> From: Paul Smith [mailto:Click here to reveal e-mail address]
->
-> Hi,
-> The Random method is commonly seeded with a DateTime instance.
->
-> Random rndQuote = new Random((int)DateTime.Now.Ticks);
->
-> ----- Original Message -----
-> From: "Thomas Stone" <Click here to reveal e-mail address>
->
-> > Hi,
-> >
-> > Problem: I am writing some tutorials and am trying to demonstrate a
-> simple
-> > example of a random-quote generator, provided in a User Control,
and
-> then
-> > used twice (or more) on an .aspx page. I want to show that the two
-> > instances of the user control on the page are independent of each
other
-> --
-> > that the output dynamically created by one is separate from that
-> produced
-> > by the other(s).
-> >
-> > I can get it to work only by resorting to old VB6 style methods,
not by
-> > using the Random class from the .NET Framework. By refreshing
enough
-> times,
-> > the quotes are shown to be independent of each other. But when
using
-> the
-> > Random class instead, the quotes are NEVER different -- they are
-> different
-> > across refreshes, but the two instances on the page are always
-> identical.
-> >
-> > Here are the beginnings of my function (located in a code-behind
file
-> for
-> > the user control) in C# and then in VB.NET, attempting to use the
-> Random
-> > class from the .NET Framework:
-> >
-> > public string RandomQuote()
-> > {
-> > int intRandomNum;
-> > Random rndQuote = new System.Random();
-> > intRandomNum = rndQuote.Next(1,4);
-> >
-> > Public Function RandomQuote() As String
-> > Dim intRandomNum as Integer
-> > Dim rndQuote As Random = New System.Random()
-> > intRandomNum = rndQuote.Next(1,4)
-> >
-> > But these attempts don't work: I get the same quote in both
instances
-> on
-> > the page each time I refresh (if one is quote A, then the other is
-> Quote
-> A,
-> > and so on). It also doesn't matter if I used NextDouble() instead
of
-> Next
-> > (). In the case where I need a random number from 1, 2, or 3, I can
do
-> > this:
-> >
-> > intRandomNum = (2) * rndQuote.NextDouble() + 1
-> >
-> > But that didn't make any difference either.
-> >
-> > What *did work* -- in VB.NET -- was to rely on the old carryover
-> methods
-> > from VB6:
-> >
-> > Public Function RandomQuote() As String
-> > Randomize
-> > Dim intRandomNum as Integer
-> > intRandomNum = (2) * Rnd + 1
-> >
-> > This worked fine. Presumably because the "randomize" keyword does
the
-> > seeding work I need?
-> >
-> > Now, the SDK suggests that to properly seed the Random class, you
can
-> > provide it a time variable in the Random(int) constructor. This is
the
-> kind
-> > of code they suggest (I think):
-> >
-> > public string RandomQuote()
-> > {
-> > int intRandomNum;
-> > Random rndQuote = new
-> System.Random(unchecked((int)DateTime.Now.Ticks));
-> > intRandomNum = rndQuote.Next(1,4);
-> >
-> > But this didn't help me either. I'm running this on my laptop (700
MHz)
-> > with not much else taking up cycle times and such. So presumably it
-> works
-> > faster than the Ticks property can be changed? I also tried the
-> Millisecond
-> > property instead of Ticks, but that didn't work either.
-> >
-> >
-> > SO... how can I get two different random numbers like this when I
am
-> using
-> > the same class twice on a page (via using a user control twice on
that
-> > page)? I can do it using the old VB6-style methods, but I can't
figure
-> out
-> > how to do it in VB.NET and C# using proper .NET Framework classes.
And
-> that
-> > is what I need -- since these are .NET Framework tutorials I am
working
-> on
-> > afterall!
-> >
-> > Thanks, as always, for any assistance you can provide!
-> >
-> > Best,
-> >
-> > Tom S.

---
[This E-mail scanned for viruses by Declude Virus]

Reply to this message...
 
    
Thomas Stone
Hi Andy,

GetHashCode worked perfectly! And I doubt I would have discovered that
solution anytime soon, so thanks a lot!

Best,

Tom S.

At 03:00 PM 7/15/2002 -0600, you wrote:
[Original message clipped]

Reply to this message...
 
 
System.DateTime
System.Random
System.Web.UI.UserControl
System.Windows.Forms.UserControl




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification