Calculate the improper integral Monte Carlo method
I need to transform it to an integral that goes from 0 to 1 (or from a to b) in order to apply the algorithm of Monte Carlo I implemented.
No, you don't.
if you have integral
∫0∞ w(x) g(x) dx
you could sample from w(x) and compute mean value of g(x) at sampled points.
You integral
∫0∞ e-x cos(x) dx
is pretty perfect for such approach - you sample from e-x and compute E[cos(x)]
Along the lines (Python 3.9, Win10 x64)
import numpy as np
rng = np.random.default_rng()
N = 1000000
U = rng.random(N)
W = -np.log(1.0 - U) # sampling exp(-x)
G = np.cos(W)
ans = np.mean(G)
print(ans)
will print something like
0.5002769491719996
And concerning your second integral, see What is the issue in my array division step?