# How to create a random generated number to my form?

**URL:** <https://forum.wixstudio.com/t/how-to-create-a-random-generated-number-to-my-form/33607>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [November 22, 2017, 6:31am UTC](https://forum.wixstudio.com/t/how-to-create-a-random-generated-number-to-my-form/33607 "2017-11-22T06:31:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_geo](https://avatars.discourse-cdn.com/v4/letter/_/c0e974/32.png) [@\_geo](https://forum.wixstudio.com/u/_geo)\
**Post date:** [November 22, 2017, 6:31am UTC](https://forum.wixstudio.com/t/how-to-create-a-random-generated-number-to-my-form/33607/1 "2017-11-22T06:31:00Z")

</div>

Hi, I hope someone will notice this. I want to create a random generated number in my form if someone is fill up form, like a ID number, cause I can’t edit the default ID database created.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/system/32/86885_2.png) [@system](https://forum.wixstudio.com/u/system)\
**Post date:** [November 22, 2017, 7:44am UTC](https://forum.wixstudio.com/t/how-to-create-a-random-generated-number-to-my-form/33607/2 "2017-11-22T07:44:43Z")

</div>

Hi geovel,

Before I answer that I have to point out that random numbers are not uniq. Of course, the larger your range is, there’s less chance to get duplication. But if you go for it anyway - when generating a new number, don’t forget to make sure that you don’t already have it as an ID in your database (and if so, generate a new one).

You can use ’ [Math.random](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) ()’ to get a random number between 0 to 1 (e.g. 0.7362768324).  
Have a look [here](https://www.kirupa.com/html5/random_numbers_js.htm) for ideas on how to get this number to be an integer, and even set a range to that integer.  
Also, [here](https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript) 's some example on how to generate a UUID string.

Liran.

---

<div class="post-metadata">

**Author:** ![\_geo](https://avatars.discourse-cdn.com/v4/letter/_/c0e974/32.png) [@\_geo](https://forum.wixstudio.com/u/_geo)\
**Post date:** [November 23, 2017, 1:31am UTC](https://forum.wixstudio.com/t/how-to-create-a-random-generated-number-to-my-form/33607/3 "2017-11-23T01:31:05Z")

</div>

Thank you Liran! would you please post of a sample code? Thank you again for the help.

---

<div class="post-metadata">

**Author:** ![tristan-breon](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/tristan-breon/32/1231_2.png) [@tristan-breon](https://forum.wixstudio.com/u/tristan-breon)\
**Post date:** [February 5, 2018, 8:02am UTC](https://forum.wixstudio.com/t/how-to-create-a-random-generated-number-to-my-form/33607/4 "2018-02-05T08:02:56Z")

</div>

I’m also interested in changing the default ID which is very unaesthetic.  
If anyone has a solution.

My items’ URL would be " /{ID}/{Title} "

I’d like the id to be something like “15795169” and the not default Mac-style address.

Thanks !

---

<div class="post-metadata">

**Author:** ![tristan-breon](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/tristan-breon/32/1231_2.png) [@tristan-breon](https://forum.wixstudio.com/u/tristan-breon)\
**Post date:** [March 25, 2018, 10:05am UTC](https://forum.wixstudio.com/t/how-to-create-a-random-generated-number-to-my-form/33607/5 "2018-03-25T10:05:57Z")

</div>

bump

---

<div class="post-metadata">

**Author:** ![code-queen](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-queen/32/696_2.png) [@code-queen](https://forum.wixstudio.com/u/code-queen)\
**Post date:** [March 25, 2018, 8:03pm UTC](https://forum.wixstudio.com/t/how-to-create-a-random-generated-number-to-my-form/33607/6 "2018-03-25T20:03:16Z")

</div>

Hi Tristan,

Here are some threads/info that may help you:

[https://www.totallycodable.com/forum/codes-for-templates/unix-time-stamp-code-epoch](https://www.totallycodable.com/forum/codes-for-templates/unix-time-stamp-code-epoch)

> **[\[SOLVED\] Random Number Generator | Velo by Wix](https://community.wix.com/velo/forum/coding-with-velo/random-number-generator)**
>
> Quick question...
> 
> Is it possible to create a button that when clicked, changes the value of a textbox on the same page to a random number between set parameters (eg between 1 and 20)?
> 
> Thanks

[https://www.wix.com/code/home/forum/questions-answers/solved-i-need-a-help-of-a-wix-expert](https://www.wix.com/code/home/forum/questions-answers/solved-i-need-a-help-of-a-wix-expert)

Once you create a random number, you can use that for your URL instead of the ID (since the ID cannot be modified in any way).

---

<div class="post-metadata">

**Author:** ![nucky](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nucky](https://forum.wixstudio.com/u/nucky)\
**Post date:** [March 25, 2018, 8:38pm UTC](https://forum.wixstudio.com/t/how-to-create-a-random-generated-number-to-my-form/33607/7 "2018-03-25T20:38:03Z")

</div>

Hi !

Thanks, I’ve read all the articles, I tried it for myself and it works :

```javascript
 $w.onReady(function () { 
  unixCode(); 
  }); 
 function unixCode (){ 
  var timeStamp = Math.floor(Date.now()/1000);
  $w('#yourElement').value = timeStamp.toString(); 
 } 

```

Only is there a way to achieve this without having to put an input element on the page ? I would like this to be a ‘backend’ feature, so no one can see it happening. And hiding it isn’t really an option since Goodle doesn’t appreciate hidden content.

Thanks !

PS : I saw 2 or 3 different types of code (including yours) what are the differences ?

---

<div class="post-metadata">

**Author:** ![bwprado](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/bwprado/32/7220_2.png) [@bwprado](https://forum.wixstudio.com/u/bwprado)\
**Post date:** [April 21, 2022, 3:09pm UTC](https://forum.wixstudio.com/t/how-to-create-a-random-generated-number-to-my-form/33607/9 "2022-04-21T15:09:01Z")

</div>

Install a NPM called [**uuid**](https://www.npmjs.com/package/uuid) , it has a method that creates Universally Unique Identifier.

Then you just import and use the method below:

```javascript
import { v4 as uuid } from 'uuid';

const newId = uuid() //this generates a uuid string

```
