Another option is to use the "Random selection" tool for selecting N randomselecting N random features features.
import processing
from qgis.utils import iface
layer = iface.activeLayer()
processing.run("qgis:randomselection", {
'INPUT' : layer,
'METHOD' : 0,
'NUMBER' : 5 -- specify a number here
})
To get more information about this algorithm, please run the following command processing.algorithmHelp("qgis:randomselection")
in the Python Console (Ctrl+Alt+P).
References: