# Generate a random string

**URL:** <https://discourse.gohugo.io/t/generate-a-random-string/26338>\
**Category:** tips & tricks\
**Created:** [June 18, 2020, 3:51am UTC](https://discourse.gohugo.io/t/generate-a-random-string/26338 "2020-06-18T03:51:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [June 18, 2020, 3:51am UTC](https://discourse.gohugo.io/t/generate-a-random-string/26338/1 "2020-06-18T03:51:14Z")

</div>

I recently fixed an [issue](https://github.com/zwbetz-gh/cupper-hugo-theme/issues/36) where I needed to generate a random string. The string had to be random, even if given the same input seed.

In short, there is a shortcode that creates “expandables” with a little js. Each expandable generates its html id based off the md5 hash of the shortcode’s `.Inner` content. Well, if two expandables had the same inner content, then things would break. My solution was:

- Get the md5 hash of the seed string
- Split that into an array
- Shuffle the array
- Convert the array back into a string

```nohighlight
{{ $seed := "foo" }}
{{ $random := delimit (shuffle (split (md5 $seed) "" )) "" }}
{{ $random }}

```

This will generate a new string each time you build the site:

```nohighlight
5b16c8cffebcc48ac4d2ad4dcfc54ed8
d45ac5c6ce8c8fdcff8dbdbace44421c
...

```

---

<div class="post-metadata">

**Author:** ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)\
**Post date:** [June 18, 2020, 11:32am UTC](https://discourse.gohugo.io/t/generate-a-random-string/26338/2 "2020-06-18T11:32:36Z")

</div>

you can concat a time string ( now as full date / time string ) to the seed
