changed to RAND_MAX in SAD