How to weight a random float? Let's assume a random variable $X$, which is preferably in a range $[a,b]$, such that $a,b,X \in \Bbb{R}$. Any float on the range is equiprobable. Now, I have a function $f(x)$ defined on the range $[a,b]$ , which maps each value of $x$ to a weight (if needed, $f$ can be divided by $(\int_a^bf)\neq0$ to make it into the probability density function $g$). How would I go about computing a variable $Y$, such that it follows the probability density of $g$, given $X$ and $f$ or $g$? 
I already tried searching the internet, but all I could find is about integers... (Also, for information, this is to be used in a program I wanna do later)
 A: What you are describing sounds like a probability density function. I'll use $g$ for what you called $f$. Suppose $g : [a,b] \to \mathbb{R}$ and
$$
\int_a^b f(t)\, dt = 1.
$$
Then you are saying: Instead of considering $X$, which is uniformly distributed on $[a,b]$ ('all numbers equiprobable'), let's suppose we have a random variable $Y$ which is distributed according to the probability density function $g$, which means that if $S \subset [a,b]$, we have
$$
\mathbb{P}\bigl( Y \in S \bigr) = \int_S g(t)\, dt.
$$


*

*There isn't really such a thing as "computing a random variable" because it is, well, random! You have to ask questions about it that are probabilistic or ask questions about it's statistics (like its expectation or variance). 

A: Similar questions have been asked here many times before,
but strangely enough I have been unable to find one that asked about this in such a general fashion.
The general method for solving this is inverse transform sampling.
Assuming $f$ is the probability density function of the distribution that you want to simulate, where $f$ is non-zero on the open interval $(a,b)$
but is zero on $(-\infty,a)$ and $(b,\infty).$
Find the cumulative distribution function $F$ by integration:
$$ F(y) = \int_a^y f(t)\,dt. $$
Since $f$ is a probability density, the range of $F$ is $[0,1].$
Next, find the inverse function of $F$ over the interval $[0,1].$ That is, find the function $F^{-1}$ such that
$F^{-1}(x) = y$ if and only if $F(y) = x.$
You do all of this once, before generating any random numbers,
and write the function $F^{-1}$ into your program.
At runtime, your program should draw the random variable $U$ from the interval $[0,1],$ and then set
$$ Y = F^{-1}(U). $$
Most software environments that offer built-in random number generation offer a RNG that gives a uniform distribution over floating-point numbers in $[0,1].$
You can use that to generate $U.$
If you are forced to use a function that generates a random number $X$ uniformly distributed in some other interval instead, such as the interval $[a,b],$
convert it to a variable uniformly distributed over $[0,1]$ so that you can use it in the formula above:
$$ U = \frac{X - a}{b - a}. $$
Note that finding $F$ is often very difficult, and finding $F^{-1}$ can also be very difficult. There may not be good "exact" methods.
In some cases (such as a normal distribution, aka a Gaussian),
specific procedures have been found that do not require computing $F^{-1}.$
But you would have to ask about a specific distribution to find out whether such a procedure is known.
Here are some other questions that have similar answers:
Generate an observation from a uniform (0,1) given a density function
Find Random Number Generator following the density $f (x) = \frac{1 + \alpha x}{2}$, $ −1 ≤ x ≤ 1$, $−1 ≤\alpha ≤ 1$
Given only uniform distribution, using mathematical transformation to derive number draw from various distributions
Using random number generator to draw from population
A: @DavidK's answer is the most general, but if you know something about $f$, and it's not too ugly, there's a very nice trick called "rejection sampling". It assumes that the function $f$ is always nonnegative (as the word "weight" suggests). 
Here's the "picture": 
Graph $f$ on the interval $[a, b]$; let's assume that the results come out between $0$ and $d$, so that the graph of $f$ lies entirely in the rectangle $R = [a, b] \times [0, d]$. 
Now do the following: 


*

*using your favorite uniform random number generator, generate a pair $(x, y)$ in the rectangle $R$. 

*Check whether $y \le f(x)$. If so, return $x$ as your "sample". If not, return to step 1.
As I say, if $f$ is nice (like $f(x) = x^2 + 1$ on the interval $[0, 1]$), then life is good: you'll end up "returning to step 1" only a few times per sample. If $f$ is bad, like $f(x) = \exp(-200x)$ on the interval $[0, 1]$, then you'll end up returning to step 1 billions of times per sample. 
A short description of "nice" is "not too spiky". 
By the way, when you pick $d$, the upper bound for the $y$-values in your rectangle, you could clearly replace it with $d+1$ or $200 d$ and everything would still work. But you'll have the fewest "rejections" (i.e., returns to step 1) if $d$ is the least upper bound of the set $\{f(x) \mid x \in [a, b \}$, i.e., if $d$ is chosen as small as possible while still making sense. For $f(x) = x^2 + 1$ on the unit interval, we see that $f(x) \le 2$, and $f(1) = 2$, so picking $d = 2$ is ideal. 
