How do I display main query posts in random order using add_filter

Should be add_action not add_filter when using pre_get_posts

add_action( 'pre_get_posts', 'thefunction');
function thefunction($query) {

    if ( ! is_admin() && $query->is_main_query() ) {
        // Not a query for an admin page.
        // It's the main query for a front end page of your site.
 
            // Let's change the query using arguments.
            $query->set( 'orderby', 'rand' );

    }

    return $query;
}