common/services/base/ActiveRecordService.php
public function getRandom( $config = [] ) {

		$offset			= $config[ 'offset' ] ?? 0;
		$limit			= $config[ 'limit' ] ?? 10;
		$conditions		= $config[ 'conditions' ] ?? null;

		// model class
		$modelClass		= static::$modelClass;

		// query generation
		$results		= [];

		$query			= $modelClass::find();

		if( isset( $conditions ) ) {

			foreach ( $conditions as $ckey => $condition ) {

				if( is_numeric( $ckey ) ) {

					$query->andWhere( $condition );

					unset( $conditions[ $ckey ] );
				}
			}

			$query->andWhere( $conditions );
		}

		// Randomise -----------

		$query = $query->orderBy( 'RAND()' );

		// Offset --------------

		if( $offset > 0 ) {

			$query->offset( $offset );
		}

		// Limit ---------------

		if( $limit > 0 ) {

			$query->limit( $limit );
		}

		$results = $query->all();

		return $results;
	}
public function getRandom( $config = [] ) {

		$offset			= $config[ 'offset' ] ?? 0;
		$limit			= $config[ 'limit' ] ?? 10;
		$conditions		= $config[ 'conditions' ] ?? null;

		// model class
		$modelClass		= static::$modelClass;

		// query generation
		$results		= [];

		$query			= $modelClass::find();

		if( isset( $conditions ) ) {

			foreach ( $conditions as $ckey => $condition ) {

				if( is_numeric( $ckey ) ) {

					$query->andWhere( $condition );

					unset( $conditions[ $ckey ] );
				}
			}

			$query->andWhere( $conditions );
		}

		// Randomise -----------

		$query = $query->orderBy( 'RAND()' );

		// Offset --------------

		if( $offset > 0 ) {

			$query->offset( $offset );
		}

		// Limit ---------------

		if( $limit > 0 ) {

			$query->limit( $limit );
		}

		$results = $query->all();

		return $results;
	}
public function getRandom ( $ config = [ ] ) { $ offset = $ config [ 'offset' ] ?? 0 ; $ limit = $ config [ 'limit' ] ?? 10 ; $ conditions = $ config [ 'conditions' ] ?? null ; // model class $ modelClass = static :: $ modelClass ; // query generation $ results = [ ] ; $ query = $ modelClass :: find ( ) ; if ( isset ( $ conditions ) ) { foreach ( $ conditions as $ ckey => $ condition ) { if ( is_numeric ( $ ckey ) ) { $ query -> andWhere ( $ condition ) ; unset ( $ conditions [ $ ckey ] ) ; } } $ query -> andWhere ( $ conditions ) ; } // Randomise ----------- $ query = $ query -> orderBy ( 'RAND()' ) ; // Offset -------------- if ( $ offset > 0 ) { $ query -> offset ( $ offset ) ; } // Limit --------------- if ( $ limit > 0 ) { $ query -> limit ( $ limit ) ; } $ results = $ query -> all ( ) ; return $ results ; }
A simple method to get random ids.
It's not efficient for tables having large number of rows.

TODO: We can make this method efficient by using random offset and limit instead of going for full table scan.
Avoid using count() to get total rows. Use Stats Table to get estimated count.

Notes: Using offset is much slower as compared to start index with limit.
A simple method to get random ids . It s not efficient for tables having large number of rows .