Randomize problem

K

kellygreer1

hi group,

I'm hoping someone here can help me. I have an issue injecting some
JavaScript onto an ASP.NET page... using
ClientScript.RegisterStartupScript.

The issue is I am using System.Random class to help generate a Random
number that will be come part of a client-side id in the HTML Dom. But
occasionally the Random.next() function seems to return the same
number. The Random is creating the same number even though it is
running in two seperate instances of a User Control (ascx file).

I tried to solve the issue by just concatenating
DateTime.Now.Ticks.ToString() to the "client id" in the JavaScript I am
sending..... and sometimes even the Ticks is the same number.

The result of this issue - is that sometimes Bars (div) that are
supposed to be contained in 2 seperate Bar Charts - all appear in the
first BarChart. Is there a way to "force" a random number?

Help. Thanks in Advance,
Kelly Greer
(e-mail address removed)
replace nospam with yahoo
 
J

Jon Shemitz

kellygreer1 said:
The issue is I am using System.Random class to help generate a Random
number that will be come part of a client-side id in the HTML Dom. But
occasionally the Random.next() function seems to return the same
number. The Random is creating the same number even though it is
running in two seperate instances of a User Control (ascx file).

Sounds like you are creating a new Random instance each time you use
it. This reads the clock, and uses it to set the random seed. Creating
two Random instances at a very short interval means that both may have
the same random seed and thus give the same random numbers.

The solution is to create a single Random instance, and pass it as a
parameter to the code that needs it.
 
P

Peter Duniho

kellygreer1 said:
[...]
The result of this issue - is that sometimes Bars (div) that are
supposed to be contained in 2 seperate Bar Charts - all appear in the
first BarChart. Is there a way to "force" a random number?

My impression that the previous reply from Jon misunderstands your problem.
Hopefully either I've misunderstood your problem or he has, and between the
two of us, your question will be answered. :)

The interpretation of your question that I have is that you are using a
single random number generator, but that the numbers generated occasionally
repeat. For example, a random sequence of numbers from 1 to 6 might look
like this:

1 5 4 2 1 3 3

You would like for the last pair of threes to not occur (of course, the
range of your random numbers may be much larger than just 1 to 6). The
repeated number IS random. It just randomly happened to be the same as the
previously picked one. You can't force it to be any *more* random...it
already is random.

A couple of thoughts:

* First, it is perfectly normal for a sequence of random numbers to
sometimes have repeated numbers. This is true whether dealing with
pseudorandom (which is what .NET is giving you) or truly random (which is
what you'd get by rolling dice, flipping coins, etc.)

* Second, if you attempt to use "Now.Ticks", as you've found if you get
that value rapidly enough, it doesn't even change between calls. If you're
looking for a way of generating unique numbers, you need to use something
that is guaranteed to change each time you look at it, like a counter you
increment each time for example.

As far as the random numbers go, there is no way to guarantee that random
numbers generated don't repeat, without doing some extra work. Random
numbers in and of themselves may very well repeat quite naturally.

It's hard to give a good answer, since it's not really clear how the numbers
are being used (in spite of your explanation). However, if you are looking
for random numbers that have *no* repeats, sequential or not, you really
want a "shuffle":

int[] rgiNumbers = new int[ciNumbers];

for (int iCur = 0; iCur < ciNumbers; iCur++)
{
rgiNumbers[iCur] = iCur;
}

for (int iCur = 0; iCur < ciNumbers; iCur++)
{
int iT, iiSwap;

iiSwap = Random(ciNumbers);
iT = rgiNumbers[iCur];
rgiNumbers[iCur] = rgiNumbers[iiSwap];
rgiNumbers[iiSwap] = iT;
}

In the above code, ciNumbers is the total number of random numbers you want
to pick, and after the second loop has completed, you can just read your
random numbers in sequence from the rgiNumbers array. Obviously, you may
want to make modifications depending on the exact range of numbers you want
to select random ones from.

If sequential repeats are the only problem, then it should be simple enough
to use a loop around the random number generation, so that you keep picking
a new random number until you get one that is not the same as the previous
one selected:

iRandomNumber = Random(iRandomMax);
while (iRandomNumber == iRandomPrevious)
{
iRandomNumber = Random(iRandomMax);
}
iRandomPrevious = iRandomNumber;

If none of the replies so far have helped, you may want to consider
restating your question more clearly.

Pete
 
K

kellygreer1

Thanks guys for the replies. I think you have given me enough
information to solve the issue.
And thanks Pete for the code. I should be able to add this in pretty
easily.

I am wondering if the correct approach might be to have the two
instances of the ascx control call a static method to get back a
'counter' number from some class... and either just use the incremented
'counter' number to guarantee a unique number, or feed the counter
number in as the seed to System.Random. Any thoughts?

I haven't done any testing down this path... But I might take the
System.Guid.NewGuid() for a spin and see what happens.

To clarify on what my code does... Each ascx file generates a Div and
then adds more smaller divs inside it as "bars" for the bar chart. But
since I am manipulting the dom through JavaScript, I needed a way to
guarantee the ids of the 2 chart areas are unique. I realize these are
other ways to tackle this issue. I just didn't understand what I was
seeing with the Random number sometimes repeating and sometimes not.
But it made more sense once I saw the same behavior in the
DateTime.Now.Ticks.

Thanks,
Kelly
 
P

Peter Duniho

kellygreer1 said:
[...]
I am wondering if the correct approach might be to have the two
instances of the ascx control call a static method to get back a
'counter' number from some class... and either just use the incremented
'counter' number to guarantee a unique number, or feed the counter
number in as the seed to System.Random. Any thoughts?

Feeding a counter into Random as a seed isn't going to change anything. No
matter what the seed is, no matter what you've done so far, Random can
always return the same number it returned the previous time.

As far as the counter goes, I am guessing that's probably more along the
lines of the right solution. I'm starting to suspect you don't really need
Random at all.
I haven't done any testing down this path... But I might take the
System.Guid.NewGuid() for a spin and see what happens.

That will certainly guarantee a unique ID. However, it's probably overkill.
To clarify on what my code does... Each ascx file generates a Div and
then adds more smaller divs inside it as "bars" for the bar chart. But
since I am manipulting the dom through JavaScript, I needed a way to
guarantee the ids of the 2 chart areas are unique.

First, please note that this isn't a web programming newsgroup. So, it's
not a good idea to assume that we know what the terms "div" and "dom" mean.
I know that I'm not sure myself what they mean. Your use of the term "div"
sounds like it might relate to the <div> HTML tag, but how that would apply
to a bar chart I don't know, nor am I really certain that's what you mean.
I just don't have the HTML background to readily comprehend those terms, and
I would guess many of the others in this newsgroup don't either.

So, it would be better if you could phrase the problem in more generic,
understandable terms. Jargon is often great for conveying complicated ideas
in few words, but in this context I think the jargon runs the risk of
obfuscating what you mean.

Now, all that said, given what you've written so far I get the impression
that the numbers you are generating are used simply for the purpose of
labeling some areas within some code-generated HTML. Is that correct?

If so, then it seems to me that simply applying some form of addressing to
the areas would be sufficient. The exact nature of that would depend on the
structure of the HTML being generated, but in the simplest case you might
simply have a single collection of areas to be labeled, and a single,
sequential counter should work fine.

If you are grouping areas somehow, then you might find it makes sense to
maintain a couple of counters. One to track the groups, and then another to
track each area within the group.

There are several benefits to doing it this way, including the fact that you
can be assured each ID will be unique, and the fact that you can easily
generate the ID in a deterministic way to access a specific area arbitrarily
(that is, if the code later on needs to manipulate the 2nd area in the 1st
group, for example, it can easily create the ID that refers to that area on
the fly as needed to access the area).

Of course, I may completely misunderstand what you're trying to accomplish.
But so far, I haven't seen anything that would suggest that using Random, or
even NewGuid, is an appropriate solution.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top