473,372 Members | 900 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,372 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 1824
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 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?

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.