You can create a shortcode which can be used anywhere inside a post.
The shortcode would be responsible for fetching a random post and displaying it.
Example code:
<?php
/*
* Plugin Name: Random Post
* Description: Display random post
* Version: 1.0
* Author: windyjonas
* Author URI: https://www.jonasnordstrom.se
*/
function display_random_post() {
ob_start();
$current_id = get_queried_object_id();
$args = [
'orderby' => 'rand',
'posts_per_page' => 1,
'ignore_sticky_posts' => true,
'post__not_in' => [ $current_id ],
];
$random_posts = get_posts( $args );
if ( ! empty( $random_posts ) ) :
$the_post = $random_posts[0]; ?>
<p><a href="https://wordpress.stackexchange.com/questions/316116/<?php echo get_permalink( $the_post->ID); ?>"><?php echo get_the_title( $the_post->ID ); ?></a></p>
<?php endif;
return ob_get_clean();
}
add_shortcode( 'random_post', 'display_random_post' );