473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 1826
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team...
9
by: abhi | last post by:
How do you get random numbers in vb .net? I want random six digit numbers. Cheers, Abhi
14
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
6
by: Anamika | last post by:
I am doing a project where I want to generate random numbers for say n people.... These numbers should be unique for n people. Two people should not have same numbers.... Also the numbers...
20
by: jjmillertime | last post by:
I'm new so i apologize if this is in the wrong spot. I'm also new to programming in C and i've been searching for quite a while on how to create a program using C that will generate two random...
12
by: Naya | last post by:
Hi. I am working on a math tutoring program which generates two random numbers (from 1 to 500) and asks the user to add them. How can I check to see whether or not they put in the correct total??...
9
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.