first implementation, correct result can be achieved
update r generation formula