Move static global variable randomDevice inside the scope of the function