Jump to content
Sign in to follow this  
Tinnu

how to get random value?

Recommended Posts

hellos

i just tried this

msgbox(0, "Random", Chr(Random(Asc("a") & 1, Asc("z") & 300, 1)))
; the above code returns emty message box
; then tried this
msgbox(0, "Random", Chr(Random(Asc("a" & 1), Asc("z" & 200), 1)))
; but this gives only a-z not with numbers.

and it did not work.

i want results like

a1

b50

x9

t4

c251

Edited by Tinnu

Share this post


Link to post
Share on other sites

i want results like

a1

b50

x9

t4

c251

Hello,

I don't really know what do you want but if you want a character between a and z (lower case) and a number between for example 1 and 500 then this could help you:

MsgBox(0,"Result",Chr(Random(Asc("a"), Asc("z"), 1)) & Random(1,500,1))

Share this post


Link to post
Share on other sites

That's not the way to go; you can't get "random" groups in that way.

What you need to do is: get random letter, get random number and then join them in a string.

MsgBox(0, "Random string", Chr(Random(Asc("a"), Asc("z"), 1))&Random(1, 500, 1))

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Func __GetRandomChars($p_sLen = 4)
    Local $L_sRand = ""
    
    For $l_ia = 0 To ($p_sLen -1)
        Switch( Random(0 , 1 , 1 ) )
            Case 1
                $L_sRand &= Chr(Random(Asc("a"), Asc("f"), 1))
            Case 0
                $L_sRand &= Random(0, 9, 1)
        EndSwitch
    Next
    
    Return $L_sRand
EndFunc


"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Share this post


Link to post
Share on other sites

ok, it's working fine.

but when i did this

$sr = Chr(Random(Asc("a"), Asc("z"), 1)) & Random(1,500,1)
While 1
    Sleep(1000)
    MsgBox(0, "Random", $sr)
WEnd

i am getting the same value again and again instead of changed one.

any solution to this?


Share this post


Link to post
Share on other sites

$sr value is constant :mellow: it doesn't get re-calculated everytime.

You need to put that "inside" the loop:

While 1
    Sleep(1000)
    MsgBox(0, "Random", Chr(Random(Asc("a"), Asc("z"), 1)) & Random(1,500,1))
WEnd

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

ok, thanks

but now i am confused because i don't know which code will work faster and is better.

can anyone tell?


Share this post


Link to post
Share on other sites

Your choice: your code giving always the same results is several miliseconds (maybe microseconds) faster than the one I posted which gives you the result you need.

I'm afraid it's up to you ... :mellow:


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...