added random function

generic can be changed to Double, so the cast below can be avoided