[HELP] RANDOM NUMBER GENERATOR
-
- Posts: 38
- Joined: Tue Mar 07, 2023 7:13 pm
- Contact:
[HELP] RANDOM NUMBER GENERATOR
How to create random number 1000 to 9999
Re: [HELP] RANDOM NUMBER GENERATOR
Hello! Unfortunately there is no direct way to get a random number in a specific range using the {{RANDOM()}} function. However, you can try the following approach:
What we have here is the function {{RANDOM("1234567890")@CUT(["0", "1"])}} repeated 4 times to obtain a number between 1000 and 9999. A more detailed explanation follows:
Code: Select all
{{RANDOM("123456789")@CUT(["0", "1"])}}{{RANDOM("1234567890")@CUT(["0", "1"])}}{{RANDOM("1234567890")@CUT(["0", "1"])}}{{RANDOM("1234567890")@CUT(["0", "1"])}}
- RANDOM("1234567890") - Randomizes the string "1234567890", to obtain a random number containing all numerals;
- The first function does not have 0 in the string to avoid having a range of 0000-9999 instead of the 1000-9999 you want.
- CUT(["0", "1"]) - Cuts the string to obtain only the first character of the string, which will be a random number between 0 and 9;
- By writing the chain of functions 4 times, we obtain a 4-digit number.
-
- Posts: 38
- Joined: Tue Mar 07, 2023 7:13 pm
- Contact:
Re: [HELP] RANDOM NUMBER GENERATOR
What if i need only 1 to 35francisco wrote: ↑Tue Jan 30, 2024 10:38 pm Hello! Unfortunately there is no direct way to get a random number in a specific range using the {{RANDOM()}} function. However, you can try the following approach:What we have here is the function {{RANDOM("1234567890")@CUT(["0", "1"])}} repeated 4 times to obtain a number between 1000 and 9999. A more detailed explanation follows:Code: Select all
{{RANDOM("123456789")@CUT(["0", "1"])}}{{RANDOM("1234567890")@CUT(["0", "1"])}}{{RANDOM("1234567890")@CUT(["0", "1"])}}{{RANDOM("1234567890")@CUT(["0", "1"])}}
- RANDOM("1234567890") - Randomizes the string "1234567890", to obtain a random number containing all numerals;
- The first function does not have 0 in the string to avoid having a range of 0000-9999 instead of the 1000-9999 you want.
- CUT(["0", "1"]) - Cuts the string to obtain only the first character of the string, which will be a random number between 0 and 9;
- By writing the chain of functions 4 times, we obtain a 4-digit number.
Re: [HELP] RANDOM NUMBER GENERATOR
Hello! Now thanks to the implementation of scripting language support in Wapka (big kudos to Admin!), you can write a Lua script to do this (Wapka Dashboard > Your Site > Your Page > Advanced function > Script):
Code: Select all
-- Initialize the seed to ensure randomness
math.randomseed(os.time())
-- Generate a random number between 1 and 35
local randomNumber = math.random(1, 35)
-- Display the generated number
print(randomNumber)
Code: Select all
-- Initialize the seed to ensure randomness
math.randomseed(os.time())
-- Generate a random number between 1000 and 9999
local randomNumber = math.random(1000, 9999)
-- Display the generated number
print(randomNumber)
-
- Site Admin
- Posts: 34
- Joined: Tue Mar 07, 2023 7:56 am
Re: [HELP] RANDOM NUMBER GENERATOR
don't use lua script now for production as math library not implemented yet
Re: [HELP] RANDOM NUMBER GENERATOR
Oh darn, sorry about that.Administrator wrote: ↑Wed Jan 31, 2024 4:28 pm don't use lua script now for production as math library not implemented yet
@shrmaprem0202 I think we will need to wait, cause I can't wrap my head around how to randomize from 1 to 35 using the RANDOM function.
-
- Site Admin
- Posts: 34
- Joined: Tue Mar 07, 2023 7:56 am
Re: [HELP] RANDOM NUMBER GENERATOR
to make random use like this:
isn't it easy
Code: Select all
{{RANDOM([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35])}}
Re: [HELP] RANDOM NUMBER GENERATOR
Yeah, it's really simple, but I think shrmaprem0202 was wanting something more programmable instead of having to type all the numbers by hand.Administrator wrote: ↑Wed Jan 31, 2024 5:18 pm to make random use like this:
isn't it easyCode: Select all
{{RANDOM([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35])}}
I should create some tool to help in this case.
-
- Posts: 38
- Joined: Tue Mar 07, 2023 7:13 pm
- Contact:
Re: [HELP] RANDOM NUMBER GENERATOR
francisco wrote: ↑Wed Jan 31, 2024 9:21 pmYeah, it's really simple, but I think shrmaprem0202 was wanting something more programmable instead of having to type all the numbers by hand.Administrator wrote: ↑Wed Jan 31, 2024 5:18 pm to make random use like this:
isn't it easyCode: Select all
{{RANDOM([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35])}}
I should create some tool to help in this case.
I want to create a random post code there i can use %total% tag in random tag
Re: [HELP] RANDOM NUMBER GENERATOR
Okay, so after some thinking I came out with this "thing" to generate random numbers between a given range (just set "start" and "end" - must be positive numbers and end > start):
Code: Select all
{{VALUE(1)@INT@SET(start)@NULL}}
{{VALUE(35)@INT@SET(end)@NULL}}
{{DATE(U)@INT@SET(time)@NULL}}
{{RANDOM("013579")@INT@SET(seed1)@NULL}}
{{RANDOM("02468")@INT@SET(seed2)@NULL}}
{{VAR(time)@DIVIDE@VAR(seed1)@INT@SET(rseed_div)@NULL}}
{{VAR(rseed_div)@MULTIPLY@VAR(seed2)@INT@SET(rseed)@NULL}}
{{VAR(end)@MINUS@VAR(start)@PLUS(1)@SET(range)@NULL}}
{{VAR(rseed)@DIVIDE@VAR(range)@INT@SET(quotient)@NULL}}
{{VAR(quotient)@MULTIPLY@VAR(range)@SET(quotient_divisor)@NULL}}
{{VAR(rseed)@MINUS@VAR(quotient_divisor)@SET(remainder)@NULL}}
{{VAR(remainder)@PLUS@VAR(start)@SET(random_number)@NULL}}
{{VAR(random_number)}}
https://www.w3resource.com/php-exercise ... ise-40.php