The variable N inside the loop is changing everytime and finally N is just a number.
To add salt and pepper noise to image, you can use imnoise function.
For more details follow the link https://www.mathworks.com/help/images/ref/imnoise.html#d120e138500