Order by rand() in yii2
Wrap it into yii\db\Expression to prevent escaping and remove array part:
use yii\db\Expression;
...
$query = User::find()
->where(['category_id' => 5])
->orderBy(new Expression('rand()'))
->limit(4);
I'm not really an expert on yii2 query builder, however, specify the ordering should help
$data= User::find()->where(['category_id'=> 5])->orderBy(['rand()' => SORT_DESC])->limit(4);