This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
en:create:functions:random_p [13.01.2015 16:54] – [Example 1] alexander.ritter | en:create:functions:random_p [13.01.2015 16:59] – [random_p()] alexander.ritter | ||
---|---|---|---|
Line 10: | Line 10: | ||
- | **Note:** Use the function [[: | + | **Note:** Use the function |
===== Example 1 ===== | ===== Example 1 ===== | ||
Line 28: | Line 28: | ||
// Save the result of the random draw | // Save the result of the random draw | ||
put(' | put(' | ||
- | // Show respective | + | // Show corresponding |
if ($number == 1) { | if ($number == 1) { | ||
text(' | text(' | ||
Line 44: | Line 44: | ||
<code php> | <code php> | ||
// Probabilities as floating-point numbers | // Probabilities as floating-point numbers | ||
- | $verteilung | + | $distribution |
</ | </ | ||
Line 51: | Line 51: | ||
<code php> | <code php> | ||
// Probabilities as ratios | // Probabilities as ratios | ||
- | $verteilung | + | $distribution |
</ | </ | ||
- | And, of course, the distribution does __not__ have to be saved in a variable | + | And, of course, the distribution does __not__ have to be saved separately |
<code php> | <code php> | ||
- | // Zahl zwischen 1 und 4 würfeln | + | // Draw number between 1 and 4 |
- | $zahl = random_p(array( | + | $number |
1 => 2, | 1 => 2, | ||
2 => 5, | 2 => 5, | ||
Line 75: | Line 75: | ||
One of the television stations watched at least rarely should now be selected at random. However, preference should be given to television stations 2, 5 and 6. | One of the television stations watched at least rarely should now be selected at random. However, preference should be given to television stations 2, 5 and 6. | ||
- | |||
- | Nun soll aus den Sendern, die mindestens selten genutzt werden, zufällig einer ausgewählt werden. Allerdings sollten die Sender 2, 5 und 6 dabei bevorzugt werden. | ||
<code php> | <code php> | ||
// Create a list in which all television stations watched appear | // Create a list in which all television stations watched appear | ||
- | $genutzt | + | $watched |
// If no station is watched, the survey is over | // If no station is watched, the survey is over | ||
- | if (count($genutzt) < 1) { | + | if (count($watched) < 1) { |
goToPage(' | goToPage(' | ||
} | } | ||
// Define probabilities for the various stations | // Define probabilities for the various stations | ||
- | $pAlle = array( | + | $pAll = array( |
1 => 10, 2 => 20, 3 => 10, 4 => 10, | 1 => 10, 2 => 20, 3 => 10, 4 => 10, | ||
5 => 20, 6 => 30, 7 => 10 | 5 => 20, 6 => 30, 7 => 10 | ||
); | ); | ||
// A distribution must now be created for the stations watched | // A distribution must now be created for the stations watched | ||
- | $pGenutzt | + | $pWatched |
- | foreach ($genutzt | + | foreach ($watched |
- | $pGenutzt[$sender] = $pAlle[$sender]; | + | $pWatched[$station] = $pAll[$station]; |
} | } | ||
// Weighted random selection from the stations watched | // Weighted random selection from the stations watched | ||
- | $fokus = random_p($pGenutzt); | + | $focus = random_p($pWatched); |
// Store the subject matter | // Store the subject matter | ||
- | put(' | + | put(' |
</ | </ |