implementing RNG algorithm