efficient substring, random sample and map code