# Show random post?

**URL:** https://meta.discourse.org/t/show-random-post/83665
**Category:** Support
**Created:** [March 23, 2018, 4:28pm UTC](https://meta.discourse.org/t/show-random-post/83665 "2018-03-23T16:28:39Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![\_vincent](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/_vincent/32/119557_2.png) [@\_vincent](https://meta.discourse.org/u/_vincent)
#### Post date: [March 23, 2018, 4:28pm UTC](https://meta.discourse.org/t/show-random-post/83665/1 "2018-03-23T16:28:39Z")

</div>

Hi,

I’m looking for a way to add a link (maybe in the header) to show a random post, preferably from a list of chosen categories.  
How would I achieve that?

Thanks!

---

<div class="post-metadata">

### Author: ![8BIT](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/8bit/32/76872_2.png) [@8BIT](https://meta.discourse.org/u/8BIT)
#### Post date: [March 23, 2018, 6:05pm UTC](https://meta.discourse.org/t/show-random-post/83665/2 "2018-03-23T18:05:14Z")

</div>

cool idea. this could help surface up older content.

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [March 23, 2018, 7:48pm UTC](https://meta.discourse.org/t/show-random-post/83665/3 "2018-03-23T19:48:09Z")

</div>

An `I'm feeling lucky` button on search?

[![](https://global.discourse-cdn.com/meta/original/4X/c/8/f/c8f32d86f94dc8aa1c4b7d2951fc45af3635289f.jpeg "I'm Feeling Lucky") ](https://www.youtube.com/watch?v=4qeGFMP-Ahw)

---

<div class="post-metadata">

### Author: ![\_vincent](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/_vincent/32/119557_2.png) [@\_vincent](https://meta.discourse.org/u/_vincent)
#### Post date: [March 23, 2018, 8:39pm UTC](https://meta.discourse.org/t/show-random-post/83665/4 "2018-03-23T20:39:31Z")

</div>

More like a “random article” link on Wikipedia.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [March 23, 2018, 9:44pm UTC](https://meta.discourse.org/t/show-random-post/83665/5 "2018-03-23T21:44:26Z")

</div>

This is what Suggested Topics at the bottom does, if you don’t have any new/unread.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [March 23, 2018, 11:47pm UTC](https://meta.discourse.org/t/show-random-post/83665/6 "2018-03-23T23:47:57Z")

</div>

Correct! With the caveat that it tries to stay in the same category you are currently in, also we disallow any suggestions older than 1 year. That can be adjusted via site setting.

---

<div class="post-metadata">

### Author: ![\_vincent](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/_vincent/32/119557_2.png) [@\_vincent](https://meta.discourse.org/u/_vincent)
#### Post date: [March 24, 2018, 10:20am UTC](https://meta.discourse.org/t/show-random-post/83665/7 "2018-03-24T10:20:07Z")

</div>

Can I build a “suggested topics” page then?

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [March 24, 2018, 10:21am UTC](https://meta.discourse.org/t/show-random-post/83665/8 "2018-03-24T10:21:15Z")

</div>

How do you envision it being different from “top”?

[https://meta.discourse.org/top](https://meta.discourse.org/top)

---

<div class="post-metadata">

### Author: ![\_vincent](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/_vincent/32/119557_2.png) [@\_vincent](https://meta.discourse.org/u/_vincent)
#### Post date: [March 24, 2018, 10:31am UTC](https://meta.discourse.org/t/show-random-post/83665/9 "2018-03-24T10:31:56Z")

</div>

> [@Mittineague](#):
>
> How do you envision it being different from “top”?

I don’t know. Page would display random topics, not top topics?  
Actually that’s not even what I asked first. What I would like to have is:

_“Random post” (link) – click → Discourse displays a random post from selected categories of the forum_

But since this is a discussion I’m glad to hear what the people have to say 🙂

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [March 24, 2018, 10:45am UTC](https://meta.discourse.org/t/show-random-post/83665/10 "2018-03-24T10:45:02Z")

</div>

I think a link to a random topic would be doable. It would need to filter to public topics to avoid giving an “oops” page. I don’t know if it would be more efficient to generate te number, check to make sure it’s OK and if not try again. Or get all the OK numbers and then randomize using that. I’m thinking the first would be better, but that’s only a guess.

---

<div class="post-metadata">

### Author: ![\_vincent](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/_vincent/32/119557_2.png) [@\_vincent](https://meta.discourse.org/u/_vincent)
#### Post date: [March 24, 2018, 10:50am UTC](https://meta.discourse.org/t/show-random-post/83665/11 "2018-03-24T10:50:08Z")

</div>

I’ll run tests with the [data explorer](https://meta.discourse.org/t/32566?silent=true). Coming up with the right request(s) might be a good start I think 🧐

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [March 24, 2018, 10:53am UTC](https://meta.discourse.org/t/show-random-post/83665/12 "2018-03-24T10:53:36Z")

</div>

I think getting the topic count(distinct ids) from badge\_posts would do for an idea of the ratio of OK to not OK when compared to the max(id) of the topics table.

---

<div class="post-metadata">

### Author: ![tophee](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tophee/32/73406_2.png) [@tophee](https://meta.discourse.org/u/tophee)
#### Post date: [March 28, 2018, 8:47am UTC](https://meta.discourse.org/t/show-random-post/83665/13 "2018-03-28T08:47:54Z")

</div>

Just an idea: instead of making it entirely random, you could make it a “show me something unusual” page or “take me outside my bubble” by limiting the random posts to posts in topics that the user has never entered.

---

<div class="post-metadata">

### Author: ![\_vincent](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/_vincent/32/119557_2.png) [@\_vincent](https://meta.discourse.org/u/_vincent)
#### Post date: [March 28, 2018, 10:40am UTC](https://meta.discourse.org/t/show-random-post/83665/14 "2018-03-28T10:40:51Z")

</div>

First steps with the [data explorer](https://meta.discourse.org/t/32566?silent=true) plugin here. I got this query up and running:

```
select * from topics
where category_id between 5 and 8
and random() < 0.005 limit 1

```

Supposing I kept it as is, how can I exploit its results on a public page?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [March 28, 2018, 2:07pm UTC](https://meta.discourse.org/t/show-random-post/83665/15 "2018-03-28T14:07:16Z")

</div>

A better query 😉

```sql
SELECT * 
FROM topics
WHERE category_id BETWEEN 5 AND 8
ORDER BY random()
LIMIT 1

```

---

<div class="post-metadata">

### Author: ![\_vincent](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/_vincent/32/119557_2.png) [@\_vincent](https://meta.discourse.org/u/_vincent)
#### Post date: [March 28, 2018, 2:43pm UTC](https://meta.discourse.org/t/show-random-post/83665/16 "2018-03-28T14:43:29Z")

</div>

I read a few articles regarding performances of order by random() queries. Your request actually takes twice as much time to return a result – but I reckon that the size of my community makes it insignificant.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [March 28, 2018, 3:48pm UTC](https://meta.discourse.org/t/show-random-post/83665/17 "2018-03-28T15:48:48Z")

</div>

> [@\_vincent](#):
>
> performances of order by random() queries.

I think it would be less expensive to pick off the top card from a shuffled category deck than it would be from topics,

I don’t care much for the “BETWEEN”. It would be like me to forget to change the numbers if the categories changed. Maybe you want some of the things I’ve NOTted but I think this would be less fragile and it seems to work well

```plaintext
WITH public AS (SELECT id AS public_cat_ids
                FROM categories
                WHERE NOT read_restricted
                AND name NOT LIKE 'Uncategorized'
                ORDER BY random()
                LIMIT 5 )
SELECT * 
FROM topics, public
WHERE category_id IN (public.public_cat_ids)
AND visible
AND NOT closed
AND NOT archived
ORDER BY random()
LIMIT 5 

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [July 26, 2020, 10:29pm UTC](https://meta.discourse.org/t/show-random-post/83665/18 "2020-07-26T22:29:56Z")

</div>



---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [September 28, 2020, 9:47pm UTC](https://meta.discourse.org/t/show-random-post/83665/19 "2020-09-28T21:47:02Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
