471,337 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

how to generate random numbers that adds up to certain value

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
Jun 27 '08 #1
5 1736
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:
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
Jun 27 '08 #2
On Tue, 20 May 2008 15:44:20 -0700, <qg**********@mailinator.comwrote:
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
Jun 27 '08 #3
DAXU wrote:
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
Jun 27 '08 #4
"DAXU" <DA**@hotmail.comwrote in message
news:07**********************************@u36g2000 prf.googlegroups.com...
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 =^.^=
Jun 27 '08 #5
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

Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Laphan | last post: by
9 posts views Thread by abhi | last post: by
12 posts views Thread by Jim Michaels | last post: by
6 posts views Thread by Anamika | last post: by
20 posts views Thread by jjmillertime | last post: by
12 posts views Thread by Naya | last post: by
24 posts views Thread by pereges | last post: by
reply views Thread by rosydwin | last post: by

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.