Community discussions

MikroTik App
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Random number between 0 and 99 or string between 00 and 99  [SOLVED]

Sat May 22, 2021 7:33 pm

search tag # rextended random number generator

Random number between 0 and 99
:global randomnum do={
    /system resource irq
    :local tmpsum 0
    :foreach i in=[find] do={:set tmpsum ($tmpsum + [get $i count])}
    :set   tmpsum [:tostr $tmpsum]
    :local lentmp [:len   $tmpsum]
    :return [:tonum [:pick $tmpsum ($lentmp - 2) $lentmp]]
}

Random string between 00 and 99
:global randomstr do={
    /system resource irq
    :local tmpsum 0
    :foreach i in=[find] do={:set tmpsum ($tmpsum + [get $i count])}
    :set   tmpsum [:tostr $tmpsum]
    :local lentmp [:len   $tmpsum]
    :return [:pick $tmpsum ($lentmp - 2) $lentmp]
}

example code

[] > :put "Generated random string $[$randomstr] and generated random number $[$randomnum]"
Generated random string 90 and generated random number 23

[] > :put "Generated random string $[$randomstr] and generated random number $[$randomnum]"
Generated random string 47 and generated random number 78

[] > :put "Generated random string $[$randomstr] and generated random number $[$randomnum]"
Generated random string 81 and generated random number 3

[] > :put "Generated random string $[$randomstr] and generated random number $[$randomnum]"
Generated random string 27 and generated random number 58

[] > :put "Generated random string $[$randomstr] and generated random number $[$randomnum]"
Generated random string 59 and generated random number 89

[] > :put "Generated random string $[$randomstr] and generated random number $[$randomnum]"
Generated random string 05 and generated random number 39
Last edited by rextended on Thu Mar 16, 2023 4:23 pm, edited 5 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Random number between 0 and 99 or string between 00 and 99

Wed Mar 15, 2023 1:11 pm

refreshed
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3169
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Random number between 0 and 99 or string between 00 and 99

Thu Mar 16, 2023 12:24 am

I guess V6 never got :rndstr or :rndnum... But in V7, those are options for this too.
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Random number between 0 and 99 or string between 00 and 99

Thu Mar 16, 2023 12:35 am

Yes, I'm all for feature equalization ;)
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3169
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Random number between 0 and 99 or string between 00 and 99

Thu Mar 16, 2023 1:06 am

Well :timestamp keeps moving along on V7, albeit slowly ;).

Bravo - IRQs...another cleaver one here.
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Random number between 0 and 99 or string between 00 and 99

Thu Mar 16, 2023 1:48 am

Thanks...


IRQs
Something really "random" and not repetable on reboot :lol:
On 2021 the random on v7 not exist or not work... ;)
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Random number between 0 and 99 or string between 00 and 99

Thu Mar 16, 2023 1:50 pm

And it's not easier to get the same from:
:local tmpsum [/system resource get cpu-load]
?
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Random number between 0 and 99 or string between 00 and 99

Thu Mar 16, 2023 2:02 pm

The CPU can have the same load all the time,
so you would never get such different numbers,
but they would always fluctuate in the usual range like 9/10/9/10/11/9/10... etc.
 
User avatar
Sertik
Member
Member
Posts: 425
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: Random number between 0 and 99 or string between 00 and 99

Thu Mar 16, 2023 4:00 pm

I agree, this is usually observed in powerful devices. In weak soho, I see it fluctuates constantly in fairly large amplitudes.

4
[@MapliteRBM33G] > :put [/system resource get cpu-load]
2
[@MapliteRBM33G] > :put [/system resource get cpu-load]
70
[@MapliteRBM33G] > :put [/system resource get cpu-load]
13
[@MapliteRBM33G] > :put [/system resource get cpu-load]
2
[@MapliteRBM33G] > :put [/system resource get cpu-load]
2
[@MapliteRBM33G] > :put [/system resource get cpu-load]
3
[@MapliteRBM33G] > :put [/system resource get cpu-load]
11
[@MapliteRBM33G] > :put [/system resource get cpu-load]
47
[@MapliteRBM33G] > :put [/system resource get cpu-load]
11
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Random number between 0 and 99 or string between 00 and 99

Thu Mar 16, 2023 4:07 pm

Yes, on your example, 3 times "2" and 2 times "11", and 2 times very close to the 2 ("3" and "4")

If they are random it could happen, even once in a million, if not more, that the same number comes out 10 times in a row and over...
Why not?
But obviously one thing is the randomness, one thing is an excessive tendency to give the same numbers....
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3169
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Random number between 0 and 99 or string between 00 and 99

Thu Mar 16, 2023 5:05 pm

You're playing with "loaded" dice using the CPU. Do you really want 99 to come up as the random number?

But yes it is one line, but do think you'll get "more randomness" from IRQ. But, yes its more lines of code.

This is why libraries for RSC are hard – everyone has difference preferences. If I actually had a need for a random number, in V6, and had internet... /tool/fetch to random.org would be even "more random". Depending on ones formatting preferences, that could be one-line too.
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Random number between 0 and 99 or string between 00 and 99

Thu Mar 16, 2023 5:36 pm

If I actually had a need for a random number, in V6, and had internet... /tool/fetch to random.org would be even "more random"
The online version can be "offline" for some reason or you can need that on closed net...

I rewrite some minutest ago the OP and convert it to two distinct functions....
Only offline 6 lines for... v6 ;)

(really.... is only one line, the next are for formatting the result...)

Who is online

Users browsing this forum: No registered users and 18 guests