473,396 Members | 1,989 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,396 software developers and data experts.

generate random w/ weighting

I need to randomly pick films from a list to automatically generate a
timetable. Each film has a priority and weighting that affects how often it
is picked. For example, say we have 3 films with the following priority and
weighting,

A 1 5%
B 2 10%
C 3 5%

B should be picked more often because of the higher weighting. If films
have equal weighting, we look at the priority. So in this example, A should
have a higher 'overall weighting' than B.

Considering these factors, what is the best way to do this? Thanks.

jobz
Jul 21 '05 #1
1 1460
jobz <jo**@xmail.com> wrote:
I need to randomly pick films from a list to automatically generate a
timetable. Each film has a priority and weighting that affects how often it
is picked. For example, say we have 3 films with the following priority and
weighting,

A 1 5%
B 2 10%
C 3 5%

B should be picked more often because of the higher weighting. If films
have equal weighting, we look at the priority. So in this example, A should
have a higher 'overall weighting' than B.

Considering these factors, what is the best way to do this? Thanks.


Well, the "percent" in the above is somewhat confusing given that they
don't add up to 100, however:

Create a list, and for each element, add the outcome and the boundary
value it's valid for, ie the previous boundary (beginning with 0) +
weight.

You'll end up with a boundary being the total of all the weights.
Generate a random number between 0 and total-1
(ie Random.NextInt(total)) then step through the list until you find
the first element which has a boundary higher than that randomly
generated number.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alexander Ross | last post by:
is there any way to get a weighted random number other than something like this: array = (1,2,2,3,3,3,4,4,4,5,5,6) and then get a random val from teh array? Alex
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...
1
by: jhicsupt via AccessMonster.com | last post by:
Bear with me – a long question but hopefully someone can help me. I have a survey which uses weighting to get the final score. I’m trying to find the best way to calculate the final score. I...
15
by: John Cassidy | last post by:
This has been driving me crazy. I've done basic C in school, but my education is mainly based on object oriented design theory where Java is our tool. For some reason, while helping a friend with a...
2
by: Henry | last post by:
Hi, How can I generate an eight digit random? Can I use the staff name to generate it? May I ask is there any sample c# code to see? Thanks
1
by: jobz | last post by:
I need to randomly pick films from a list to automatically generate a timetable. Each film has a priority and weighting that affects how often it is picked. For example, say we have 3 films with...
6
by: comp.lang.php | last post by:
/** * Generate the random security image * * @access public * @param $willUseFilePath (default false) boolean to determine if you will be using a file path * @param mixed $filePath (optional)...
9
by: MyInfoStation | last post by:
Hi all, I am a newbie to Python and would like to genereate some numbers according to geometric distribution. However, the Python Random package seems do not have implemented functionality. I am...
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...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.