Update Gallery Randomly in every hour with no duplicates?

You’re trying to retrieve a set of posts, and display them, in a random order.

Firstly:

  • Indent your code to prevent bugs, as it reads, there is a closing endif; in your questions code but no if statement was ever opened. If you had indented correctly this would be trivial to spot, good editors will do the indenting for you automatically
  • $do_not_duplicate is undefined
  • Never under any circumstances use query_post. Whatever site or person told you to use it was mistaken, and it encourages bad practices that lead to common and easily avoidable problems, e.g. pagination issues

If you are trying to replace the posts listed on a page with a different list of posts that are randomly ordered, then you should use the pre_get_posts filter to modify the main query rather wasting time by throwing it away and putting a new one in its place.

If you’re trying to add a list of posts in a sidebar or at the end of a post then use WP_Query, e.g.:

$parameters = array( ... );
$query = new WP_Query( $parameters );
if ( $query->have_posts() ) {
    // posts were found
    while ( $query->have_posts() ) {
        // set the current post, then display it
        $query->the_post();
    }
    // cleanup afterwards
    wp_reset_postdata();
} else {
    // nothing was found
}

The codex page for WP_Query contains a full list of all the parameters it takes and their usage, including orderby. Specifically it mentions a rand value to select random posts. These options are not SQL, so placing raw SQL for randomness inside it will not work, and is not advised.

If you wish to prevent duplicates, you should use post__not_in to specify which posts not to retrieve, but as you’re trying to modify an archive or main query I don’t believe this is of help to you.

Instead something similar to this should help:

add_filter( 'pre_get_posts', function ( $query ) {
    if ( $query->is_main_query() && !is_admin() ) {
        $query->set( 'order', 'rand' );
    }
} );

That snippet will make all main queries on the frontend randomly ordered, you will need to add an additional check to make sure only the page/archive you want is ordered randomly, but since you haven’t mentioned where your query is being made or why, I can only guess at that, but it might make an interesting new question