Connecting Tech Pros Worldwide Forums | Help | Site Map

how to generate random numbers that adds up to certain value

DAXU
Guest
 
Posts: n/a
#1: Jun 27 '08
Hi,
I need to generate a fixed number of random integers that their
summary equals to certain values.
For example, the scenario can be:
simulate user hits (e.g. 20000) within a hour (3600 seconds).
the hits need to be random (e.g. five can be in second one, then 0
for seconds 2-4, another 10 at second 5 and so on)

I haven't really figure out the best way to do this.

Can someone help me?

Many Thanks

Jerry

qglyirnyfgfo@mailinator.com
Guest
 
Posts: n/a
#2: Jun 27 '08

re: how to generate random numbers that adds up to certain value


Well, I am getting ready to live work so I slapped something together
hopping it will do what you need and it not buggy :)

class Program
{
static void Main(string[] args)
{
int hits = 2000;
int seconds = 3600;

int[] randomHits = new int[seconds];

Random rd = new Random();
int accumulatedHits = 0;
while (true)
{
int randomSec = (int)(rd.NextDouble() * (double)(seconds -
1));
randomHits[randomSec] += 1;
accumulatedHits++;

if (accumulatedHits == hits)
break;
}
}






On May 20, 4:35*pm, DAXU <D...@hotmail.comwrote:
Quote:
Hi,
I need to generate a fixed number of random integers that their
summary equals to certain values.
For example, the scenario can be:
simulate user hits (e.g. 20000) within a hour (3600 seconds).
the hits need to be random (e.g. *five can be in second one, then 0
for seconds 2-4, another 10 at second 5 and so on)
>
I haven't really figure out the best way to do this.
>
Can someone help me?
>
Many Thanks
>
Jerry
Peter Duniho
Guest
 
Posts: n/a
#3: Jun 27 '08

re: how to generate random numbers that adds up to certain value


On Tue, 20 May 2008 15:44:20 -0700, <qglyirnyfgfo@mailinator.comwrote:
Quote:
Well, I am getting ready to live work so I slapped something together
hopping it will do what you need and it not buggy :)
I only found one bug: no need to subtract 1 from "seconds", as
Random.NextDouble() returns [0, 1), not [0, 1] (that is, it will never
actually return 1.0). By subtracting one, you guarantee that second 3599
will never be populated, which isn't correct.

To the OP: see what I mean? This is an entirely different solution, with
very different results, and yet it meets at least as much of the criteria
stated as any other solution could (including the one I posted).

That said, I'd clean it up a bit. Without changing too much of the
original style:

//...assume initialization, etc.

while (accumulatedHits++ < hits)
{
randomHits[rd.Next(seconds)]++;
}

Of course, as "hits" gets larger (and the proposed count was 20000, not
2000), the relative uniformity of distribution of hits over the seconds
becomes very even. Even with as few as a few hundred, it's reasonably
even (albeit sparse), and at 20000, it's definitely starting to get
difficult to tell the difference between incrementing per-second counters
randomly, and just initializing them all to "hits / seconds".

But of course, this solution _does_ in fact meet at least one version of
what the OP asked for. That's the problem with ambiguous problem
descriptions. :)

Pete
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
 
Posts: n/a
#4: Jun 27 '08

re: how to generate random numbers that adds up to certain value


DAXU wrote:
Quote:
I need to generate a fixed number of random integers that their
summary equals to certain values.
For example, the scenario can be:
simulate user hits (e.g. 20000) within a hour (3600 seconds).
the hits need to be random (e.g. five can be in second one, then 0
for seconds 2-4, another 10 at second 5 and so on)
>
I haven't really figure out the best way to do this.
I would do something like:

Random rng = new Random();
int[] a = new int[3600];
for(int i = 0; i < 20000; i++)
{
a[rng.Next(a.Length)]++;
}

Arne
colin
Guest
 
Posts: n/a
#5: Jun 27 '08

re: how to generate random numbers that adds up to certain value


"DAXU" <DAXU@hotmail.comwrote in message
news:0793ca6d-38ec-4b74-9be4-5be3973fb821@u36g2000prf.googlegroups.com...
Quote:
Hi,
I need to generate a fixed number of random integers that their
summary equals to certain values.
For example, the scenario can be:
simulate user hits (e.g. 20000) within a hour (3600 seconds).
the hits need to be random (e.g. five can be in second one, then 0
for seconds 2-4, another 10 at second 5 and so on)
>
I haven't really figure out the best way to do this.
>
Can someone help me?
>
Many Thanks
>
Jerry
you could generate a list of random numbers the usual way,
then sum them, then adjust them by the necessary amount,
either by multiplying them all by the right factor,
or by adding or subtracting 1 from random numbers.

Colin =^.^=


DAXU
Guest
 
Posts: n/a
#6: Jun 27 '08

re: how to generate random numbers that adds up to certain value


Thanks for everyone's reply.

It is not a scientific research and the requirement is not strict
(used as am internal tool). I think the solutions here are already
enough for me. Many Thanks

Closed Thread