473,664 Members | 3,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

random numbers

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
Jul 17 '05 #1
2 4824
uhh sorry my english suxs but i dunno what u mean maybe reading this
will help:
http://nl.php.net/manual/nl/function.array-rand.php
with array_rand() u can get just random stuff out of a array and else
try reading this just the basic random stuff:
http://nl.php.net/manual/nl/function.rand.php

good luck

On Sat, 11 Oct 2003 23:04:33 GMT, "Alexander Ross"
<al******@bleen .net> wrote:
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


Jul 17 '05 #2
On Sat, 11 Oct 2003 23:04:33 GMT, "Alexander Ross" <al******@bleen .net> wrote:
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?


Yes, but that's probably the simplest. You can produce that array from an
array of weights if you wanted.

<pre>
<?php
function weighted_rand($ weights) {
foreach ($weights as $value => $weight)
for ($i=0; $i<$weight; $i++)
$values[] = $value;

return $values[mt_rand(0, count($values)-1)];
}

$num_runs = 40000;
$weights = array(1=>1, 2=>2, 3=>3);

for ($i=0; $i < $num_runs; $i++) {
$rand = weighted_rand($ weights);

if (isset($results[$rand]))
$results[$rand]++;
else
$results[$rand] = 1;
}
ksort($results) ;

$total_weight = 0;
foreach ($weights as $weight)
$total_weight += $weight;

foreach ($results as $value => $freq) {
printf("%d : expected %8d (%3.2f%%), actual %8d (%3.2f%%)\n",
$value,
$num_runs * ($weights[$value] / $total_weight),
($weights[$value] / $total_weight) * 100.0,
$freq, $freq/$num_runs * 100.0);
}
?>
</pre>

Outputs:

1 : expected 6666 ( 16.67%), actual 6624 ( 16.56%)
2 : expected 13333 ( 33.33%), actual 13383 ( 33.46%)
3 : expected 20000 ( 50.00%), actual 19993 ( 49.98%)

Or you could skip creating the array with repeated values and work directly
off the weight array:

function weighted_rand($ weights) {
$total_weight = 0;
foreach ($weights as $weight)
$total_weight += $weight;

$rand = mt_rand(1, $total_weight);

$total_weight = 0;
foreach ($weights as $value => $weight) {
$total_weight += $weight;
if ($rand <= $total_weight)
return $value;
}
}

Output to make sure it's still weighting correctly:

1 : expected 6666 ( 16.67%), actual 6554 ( 16.38%)
2 : expected 13333 ( 33.33%), actual 13352 ( 33.38%)
3 : expected 20000 ( 50.00%), actual 20094 ( 50.23%)

--
Andy Hassall (an**@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #3

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

Similar topics

10
11946
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was using to get random numbers was Random rn = new Random(System.currentTimeMillis()); but it seems that the system doesn't update the milliseconds often enough to cause a true randomaziation (i ran just the System.currentTimeMillis() in a
3
7372
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I have utilised the following random number source code http://www.agner.org/random/ What I have found is that there is a problem with seeding. The code generates a seed based on time(0). I have found that I need to increment
21
3008
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX. Thank you for your help
5
2394
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they can go down similar paths, but not be the same. I will implement code later to make sure i they go more than 10 apart from each other that they are moved closer together, but this is what I have so far, and when the program is run, the two random...
104
5129
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
12
5219
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 RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
21
13501
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
13
2802
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num )
6
11741
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program that will randomly generate 10 unique random numbers. Your job is to write a program that produces random permutations of the numbers 1 to 10. “Permutation” is a mathematical name for an arrangement. For example, there are six permutations of the...
24
7207
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 uniformly distributed and unique numbers ?
0
8437
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8636
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.