Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
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...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
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...
Vote that this is a GOOD answer...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
    
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...
Vote that this is a GOOD answer...
 
 
    
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...
Vote that this is a GOOD answer...
 
Email Archiving and Email Filing - what’s the difference?
Web-based task/todo list management
 
    
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...
Vote that this is a GOOD answer...
 
Open source windows
The Law Society’s guidelines on e-mail management
 
    
Payal Amin
You use unchecked((int)DateTime.Now.Ticks to generate the first number and use
~unchecked((int)DateTime.Now.Ticks to generate the next number, may be you got the same number but it will probably generate two different numbers!!!

Hope that this will help you!!

--------------------------------
From: Payal Amin
Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Payal Amin
You use unchecked((int)DateTime.Now.Ticks to generate the first number and use
~unchecked((int)DateTime.Now.Ticks to generate the next number, may be you got the same number but it will probably generate two different numbers!!!

Hope that this will help you!!

--------------------------------
From: Payal Amin
Reply to this message...
Vote that this is a GOOD answer...
 
Google Docs… no.
Twitter Elite
 
    
Payal Amin
You use unchecked((int)DateTime.Now.Ticks to generate the first number and use
~unchecked((int)DateTime.Now.Ticks to generate the next number, may be you got the same number but it will probably generate two different numbers!!!

Hope that this will help you!!

--------------------------------
From: Payal Amin
Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Payal Amin
You use unchecked((int)DateTime.Now.Ticks to generate the first number and use
~unchecked((int)DateTime.Now.Ticks to generate the next number, may be you got the same number but it will probably generate two different numbers!!!

Hope that this will help you!!

--------------------------------
From: Payal Amin
Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Payal Amin
You use unchecked((int)DateTime.Now.Ticks to generate the first number and use
~unchecked((int)DateTime.Now.Ticks to generate the next number, may be you got the same number but it will probably generate two different numbers!!!

Hope that this will help you!!

--------------------------------
From: Payal Amin
Reply to this message...
Vote that this is a GOOD answer...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
 
System.DateTime
System.Random
System.Web.UI.UserControl
System.Windows.Forms.UserControl




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734