changed random generate algorithm