# I would like to generate a random number on my site without having to press a button

**URL:** <https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [June 26, 2020, 10:00pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371 "2020-06-26T22:00:12Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![greensteinrya08](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/greensteinrya08/32/21352_2.png) [@greensteinrya08](https://forum.wixstudio.com/u/greensteinrya08)\
**Post date:** [June 26, 2020, 10:00pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/1 "2020-06-26T22:00:12Z")

</div>

I want to have a 4 digit (0000-9999) random number on my page WITHOUT having to press a button.

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [June 26, 2020, 10:08pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/2 "2020-06-26T22:08:13Z")

</div>

Hello ryan greenstein,

take a look here…  
[https://russian-dima.wixsite.com/meinewebsite/random-numbers-strings](https://russian-dima.wixsite.com/meinewebsite/random-numbers-strings)  
…  
[https://webiya.wixsite.com/rnd-number-example](https://webiya.wixsite.com/rnd-number-example)

If you want it happen immediately after site is loaded without a buttton, then just start it in the onLoad function.

```javascript
$w.onReady(function () { })

```

---

<div class="post-metadata">

**Author:** ![greensteinrya08](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/greensteinrya08/32/21352_2.png) [@greensteinrya08](https://forum.wixstudio.com/u/greensteinrya08)\
**Post date:** [June 26, 2020, 10:42pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/3 "2020-06-26T22:42:10Z")

</div>

Thank you russian-dima, I actually looked at both of those. I tried them, but it wouldn’t work for me.

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [June 26, 2020, 10:43pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/4 "2020-06-26T22:43:47Z")

</div>

Why?

---

<div class="post-metadata">

**Author:** ![greensteinrya08](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/greensteinrya08/32/21352_2.png) [@greensteinrya08](https://forum.wixstudio.com/u/greensteinrya08)\
**Post date:** [June 26, 2020, 10:44pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/5 "2020-06-26T22:44:55Z")

</div>

So… I don’t know. I matched all of the #button1’s and the #text1’s but it still didn’t work.

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [June 26, 2020, 10:45pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/6 "2020-06-26T22:45:35Z")

</div>

Show your relevant code…

---

<div class="post-metadata">

**Author:** ![greensteinrya08](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/greensteinrya08/32/21352_2.png) [@greensteinrya08](https://forum.wixstudio.com/u/greensteinrya08)\
**Post date:** [June 26, 2020, 10:45pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/7 "2020-06-26T22:45:53Z")

</div>

Ok

---

<div class="post-metadata">

**Author:** ![greensteinrya08](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/greensteinrya08/32/21352_2.png) [@greensteinrya08](https://forum.wixstudio.com/u/greensteinrya08)\
**Post date:** [June 26, 2020, 10:46pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/8 "2020-06-26T22:46:05Z")

</div>

**function** getRandomInRange(min, max) {  
min = Math.ceil(min);  
max = Math.floor(max);  
**return** Math.floor(Math.random() \* (max - min + 1 )) + min;  
}

**function** getRandomString() {  
**return** Math.random().toString( 36 ).substr( 2 , 8 );  
}

$w.onReady( **function** () {  
$w( ‘#JOIN’ ).onClick(() =\> {  
‘#RandomNumber’ .text = getRandomInRange( 0 , 9999 ).toString();  
$w( ‘#ID’ ).text = getRandomString().toString();

```
}); 

```

});

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [June 26, 2020, 10:50pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/9 "2020-06-26T22:50:24Z")

</div>

Check it out…

```javascript
function getRandomInRange(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getRandomString() {
 return Math.random().toString(36).substr(2, 8);
}

$w.onReady(function () {
    $w('#JOIN').onClick(() => {
    $w('#RandomNumber').text = getRandomInRange(0, 9999).toString();
    $w('#ID').text = getRandomString().toString();

    });
});

```

---

<div class="post-metadata">

**Author:** ![greensteinrya08](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/greensteinrya08/32/21352_2.png) [@greensteinrya08](https://forum.wixstudio.com/u/greensteinrya08)\
**Post date:** [June 26, 2020, 10:51pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/10 "2020-06-26T22:51:35Z")

</div>

Ok

---

<div class="post-metadata">

**Author:** ![greensteinrya08](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/greensteinrya08/32/21352_2.png) [@greensteinrya08](https://forum.wixstudio.com/u/greensteinrya08)\
**Post date:** [June 26, 2020, 10:51pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/11 "2020-06-26T22:51:49Z")

</div>

Thank you so much!

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [June 26, 2020, 10:53pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/12 "2020-06-26T22:53:10Z")

</div>

It works?  
If you liked my answer —\> think about to give a like xD​:grin: (or even “BEST-ANSWER”) ? 😋

---

<div class="post-metadata">

**Author:** ![greensteinrya08](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/greensteinrya08/32/21352_2.png) [@greensteinrya08](https://forum.wixstudio.com/u/greensteinrya08)\
**Post date:** [June 26, 2020, 11:11pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/13 "2020-06-26T23:11:19Z")

</div>

I did and everything to rate it the best. Have a good night! (in est)

---

<div class="post-metadata">

**Author:** ![greensteinrya08](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/greensteinrya08/32/21352_2.png) [@greensteinrya08](https://forum.wixstudio.com/u/greensteinrya08)\
**Post date:** [June 27, 2020, 12:01am UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/14 "2020-06-27T00:01:33Z")

</div>

i did all

---

<div class="post-metadata">

**Author:** ![keepstruggle786123](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/keepstruggle786123/32/32539_2.png) [@keepstruggle786123](https://forum.wixstudio.com/u/keepstruggle786123)\
**Post date:** [March 8, 2022, 6:06pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/15 "2022-03-08T18:06:32Z")

</div>

I want to genrate 10 digit rendom number

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [March 24, 2024, 3:51pm UTC](https://forum.wixstudio.com/t/i-would-like-to-generate-a-random-number-on-my-site-without-having-to-press-a-button/31371/17 "2024-03-24T15:51:16Z")

</div>

Nice idea! 😊 Could be very useful.

The IDEA was gone! 😁 😊 → but i already memorized it 😁
