Random Number Generation

I need generate random number between 100000000 - 999999999 .
How can I create random numbers ? anyone guide me ?

use PHP
echo(rand(100000000,999999999));

Yeap I can create via PHP but I want to find dfferent way for wappler usage .
maybe this could be feature request . Because I didnt find any way for number generation.

you can use the php generated in App Connect.

<dmx-value id="myNumber" value="<?php echo(rand(100000000,999999999)) ?>"></dmx-value>

{{ myNumber.toNumber() }}
5 Likes

maybe it should be {{ myNumber.value.toNumber() }}
Tried it in Wappler and this is how I managed to display the value :slight_smile:

1 Like

Anyone able to get this to work? I can't get it to display the value on the page?

<dmx-value id="myNumber" dmx-bind:value="<?php echo(rand(1000000000,9999999999)) ?>"></dmx-value>{{myNumber.value.toNumber()}}

When I view the source code on the live page it shows it is generating the number but it doesn't display on the page?

<dmx-value id="myNumber" dmx-bind:value="1345429548"></dmx-value>

Solved it by removing the toNumber conversion. This worked …

{{myNumber.value}}

This was so easy to do in Wappler! Loving Wappler! :wink:

The difference is in how the value is set. value= sets a static value which is always a string, dmx-bind:value= sets a dynamic value using an expression. Since the expression in your case is just a number, the output is also a number, so the toNumber() is not needed.

2 Likes

Hi, sorry to revive this topic,
How to replicate something like this in NodeJS, maybe some elegant way using Wappler functions?
Thanks

Do you need this on your pages or on the server side?

If you use it serverside, you could create a php file with your own formatters in this directory: dmxConnectLib/lib/formatters/ownformatters.php

In this file u can create functions you can afterwards use in the same way as already existing formatters like .toString() or .toNumber() or whatever.

code which belongs to ownformatters.php:

function formatter_getrandomnumber(){
return rand(100000000,999999999);
}

Then in any server action you can create random numbers like this: {{.getrandomnumber()}}

2 Likes

Hi, in pages.
Is possible?

Oh and don’t forget to include the following at the beginning of the file:
namespace lib\core;

So the whole code is as follows:

<?php

namespace lib\core;

function formatter_getrandomnumber(){
return rand(100000000,999999999);
}

function formatter_anyotherformatter($val){
//do anything
return $val
}

?>
1 Like

Sorry to revive this topic but did someone find the way to generate random number with nodejs ??

Hi @Chackmool, do you need to generate these random numbers on the server side or client side?

Both sides. Server and client

On the server side, I use a custom query to generate random numbers (6). Select SQL as the syntax type in the Database Custom Query builder.

SELECT LPAD(FLOOR(RAND() * 999999.99), 6, '0') AS RandomNumber;

I haven’t generated a random number on the client side, but a JS function like this can generate a 6 digit random number (StackOverflow).

Math.floor(100000 + Math.random() * 900000)