  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # How to adjust the distribution of values in a random data stream?  » development » How to adjust the distribution of values in a random data stream?

By : abhijit
Date : October 28 2020, 04:55 PM
this will help There is a well-known device due to Von Neumann for turning an unfair coin into a fair coin. We can use this device to solve our problem here.
Repeatedly draw two bits from your biased source until you obtain a pair for which the bits are different. Now return the first bit, discarding the second. This produces an unbiased source. The reason this works is because regardless of the source, the probability of a 01 is the same as a probability of a 10. Therefore the probability of a 0 conditional on 01 or 10 is 1/2 and the probability of a 1 conditional on 01 or 10 is 1/2. code : ## Adjust algorithm for generating random strength values

By : Hayden Dee
Date : March 29 2020, 07:55 AM
this will help The values 1.033 and -0.45 in the original code are the magic numbers that provide the scale 1.1 - 9.9. You should get the same results if you pass in 1.1 and 9.9 as the parameters \$low and \$high in the following code.
code :
``````function getRandomStrength(\$low, \$high) {
// TODO: validate the input
\$ln_low = log( \$low, M_E );
\$ln_high = log( \$high, M_E );
\$scale = \$ln_high - \$ln_low;

\$rand = ( mt_rand() / mt_getrandmax() ) * \$scale + \$ln_low;
\$value = round( pow( M_E, \$rand), 1 );
return \$value;
}
`````` ## Random values with non-uniform distribution

By : Sandamal Perera
Date : March 29 2020, 07:55 AM
Any of those help In your solution you generate an accumulated probability vector, which is very useful.
I have two suggestions for improvement: ## Generating random floating-point values based on random bit stream

By : user2740116
Date : March 29 2020, 07:55 AM
it should still fix some issue I don't think I'll ever be convinced that you actually need this, but it was fun to write.
code :
``````#include <stdint.h>

#include <cmath>
#include <cstdio>

FILE* devurandom;

bool geometric(int x) {
// returns true with probability min(2^-x, 1)
if (x <= 0) return true;
while (1) {
uint8_t r;
if (x < 8) {
return (r & ((1 << x) - 1)) == 0;
} else if (r != 0) {
return false;
}
x -= 8;
}
}

double uniform(double a, double b) {
// requires IEEE doubles and 0.0 < a < b < inf and a normal
// implicitly computes a uniform random real y in [a, b)
// and returns the greatest double x such that x <= y
union {
double f;
uint64_t u;
} convert;
convert.f = a;
uint64_t a_bits = convert.u;
convert.f = b;
uint64_t b_bits = convert.u;
uint64_t mask = b_bits - a_bits;
int b_exp;
frexp(b, &b_exp);
while (1) {
// sample uniform x_bits in [a_bits, b_bits)
uint64_t x_bits;
x_bits += a_bits;
if (x_bits >= b_bits) continue;
double x;
convert.u = x_bits;
x = convert.f;
// accept x with probability proportional to 2^x_exp
int x_exp;
frexp(x, &x_exp);
if (geometric(b_exp - x_exp)) return x;
}
}

int main() {
devurandom = fopen("/dev/urandom", "r");
for (int i = 0; i < 100000; ++i) {
printf("%.17g\n", uniform(1.0 - 1e-15, 1.0 + 1e-15));
}
}
`````` ## random values with uneven distribution

By : Harsh Patel
Date : March 29 2020, 07:55 AM
it fixes the issue If you have your words in a list words, random.choice(words) will uniformly randomly select a word from the list.
If you only have word lengths, I'd store the lengths and counts in a list:
code :
``````len_counts = [
(1, 24), # 24 1-length words
(2, 39), # 39 2-length words
...
]
`````` ## R - random distribution with predefined min, max, mean, and sd values

By : Faiz
Date : March 29 2020, 07:55 AM
I wish this helpful for you Discussion:
Hi. It is very interesting problem. It needs quite an effort to be solved properly and not always solution can be found. 