Author Topic: Random Character  (Read 14320 times)

ScriptBasic

  • Guest
Random Character
« on: September 24, 2016, 08:20:48 AM »
Challenge:

Write a void argument function that will return a random character within the ranges of [A-Z] [a-z] [0-9].


Mopz

  • Guest
Re: Random Character
« Reply #1 on: September 24, 2016, 10:54:59 AM »
Code: [Select]
function RandomChar$()
r = rnd(10 + 26 + 26)
if r < 10;          return chr(asc("0") + r)
elseif r < 10 + 26; return chr(asc("A") + r - 10)
else;               return chr(asc("a") + r - 10 - 26)
endif
endfunc

I'm guessing you have some ScriptBasic ace up your sleeve? :D

Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #2 on: September 24, 2016, 12:59:15 PM »
IIF is the function:

Code: [Select]
#AppType Console
Randomize
Print $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("0"), %Asc("9"))), $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("A"), %Asc("Z"))), $Chr(%RandInt(%Asc("a"), %Asc("z")))))
Print $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("0"), %Asc("9"))), $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("A"), %Asc("Z"))), $Chr(%RandInt(%Asc("a"), %Asc("z")))))
Print $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("0"), %Asc("9"))), $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("A"), %Asc("Z"))), $Chr(%RandInt(%Asc("a"), %Asc("z")))))
Print $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("0"), %Asc("9"))), $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("A"), %Asc("Z"))), $Chr(%RandInt(%Asc("a"), %Asc("z")))))
Print $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("0"), %Asc("9"))), $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("A"), %Asc("Z"))), $Chr(%RandInt(%Asc("a"), %Asc("z")))))
Print $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("0"), %Asc("9"))), $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("A"), %Asc("Z"))), $Chr(%RandInt(%Asc("a"), %Asc("z")))))
Print $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("0"), %Asc("9"))), $IIf(%RandInt(0, 1), $Chr(%RandInt(%Asc("A"), %Asc("Z"))), $Chr(%RandInt(%Asc("a"), %Asc("z")))))
Pause

Typical outputs:

U
s
L
7
i
0
q

Press any key to continue...


or

7
q
n
7
i
0
s

Press any key to continue...


I've no idea tho how even the distribution would be.

Mopz

  • Guest
Re: Random Character
« Reply #3 on: September 24, 2016, 01:21:25 PM »
C'mon, John, show us yours!

Tomaaz

  • Guest
Re: Random Character
« Reply #4 on: September 24, 2016, 02:54:19 PM »
Code: [Select]
from random import *
def randchar():
return "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"[randrange(61)]

Mopz

  • Guest
Re: Random Character
« Reply #5 on: September 24, 2016, 03:02:38 PM »
Code: [Select]
from random import *
def randchar():
return "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"[randrange(61)]

Ah, that's a nice approach, very elegant! Why didn't I think of that :)
« Last Edit: September 24, 2016, 03:05:15 PM by Marcus »

Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #6 on: September 24, 2016, 03:26:28 PM »
Yeah, that one would have a much better distribution profile.

Cf.:

Code: [Select]
#AppType Console
Randomize

Print "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"{RandInt(1, 62)}
Print "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"{RandInt(1, 62)}
Print "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"{RandInt(1, 62)}
Print "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"{RandInt(1, 62)}
Print "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"{RandInt(1, 62)}
Print "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"{RandInt(1, 62)}
Print "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"{RandInt(1, 62)}
Print "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"{RandInt(1, 62)}

Pause

Typical output:

f
M
k
Y
Q
0
s
U

Press any key to continue...

Tomaaz

  • Guest
Re: Random Character
« Reply #7 on: September 24, 2016, 03:32:52 PM »
Ah, that's a nice approach, very elegant! Why didn't I think of that :)

What about this one? ;)

Code: [Select]
def randchar
(("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a)[rand(62)]
end

Not as ellegant and clear, but makes easier to modify the ranges. ;)

Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #8 on: September 24, 2016, 03:44:28 PM »
Attempt #3:

Code: [Select]
#AppType Console
Randomize

Print Peek(@"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789" + RandInt(0, 61), $1)
Print Peek(@"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789" + RandInt(0, 61), $1)
Print Peek(@"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789" + RandInt(0, 61), $1)
Print Peek(@"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789" + RandInt(0, 61), $1)
Print Peek(@"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789" + RandInt(0, 61), $1)
Print Peek(@"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789" + RandInt(0, 61), $1)
Print Peek(@"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789" + RandInt(0, 61), $1)
Print Peek(@"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789" + RandInt(0, 61), $1)

Pause

Typical output:

v
1
P
t
1
W
R
8

Press any key to continue



In attempts #2 and #3, the string may be either a literal or a variable.

Tomaaz

  • Guest
Re: Random Character
« Reply #9 on: September 24, 2016, 04:01:24 PM »
Where is John???  ;D

Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #10 on: September 24, 2016, 04:02:03 PM »
Sleeping. He's an U.S. citizen, after all... ;)

Mopz

  • Guest
Re: Random Character
« Reply #11 on: September 24, 2016, 04:10:25 PM »
Sleeping. He's an U.S. citizen, after all... ;)

Dammit, you can't just start a war and then disappear!

Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #12 on: September 24, 2016, 04:12:38 PM »
Quote
Dammit, you can't just start a war and then disappear!

Unless you've been shot dead by a stray bullet. ;D


In the meantime, attempt #4:

Code: [Select]
#AppType Console
Randomize

Print randchar()
Print randchar()
Print randchar()
Print randchar()
Print randchar()
Print randchar()
Print randchar()
Print randchar()

Pause

Function randchar()
  Static a[] = Split("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
  Static b[] = Split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", ",")
  Static c[] = Split("0,1,2,3,4,5,6,7,8,9", ",")
  Static d[] = ArrayMerge(a, b, c)
  Static e   = Count(d) - 1
 
  Return d[RandInt(0, e)]
End Function

Typical output:

C
m
f
R
O
1
B
T

Press any key to continue...


Note that all statics will be initialized only once at app start. Thereafter all runtime calls will only Return a fast random char from pre-initialized d[].

jj2007

  • Guest
Re: Random Character
« Reply #13 on: September 24, 2016, 04:43:28 PM »
Print "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"{RandInt(1, 62)}

Nice but not basic enough, Mike ;)

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  For_ ct=0 To 999
      Print Mid$("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789", Rand(62)+1, 1)
  Next
EndOfCode

ScriptBasic

  • Guest
Re: Random Character
« Reply #14 on: September 24, 2016, 04:44:44 PM »
Quote
C'mon, John, show us yours!

Code: [Select]
RANDOMIZE(NOW)

FUNCTION RC
  n = RND % 62
  IF n < 10 THEN
    RC = n
  ELSE IF n < 36 THEN
    RC = CHR(n + 55)
  ELSE
    RC = CHR(n + 61)
  END IF
END FUNCTION

PRINT RC(),"\n"


jrs@laptop:~/sb/sb22/test$ scriba ranchr.sb
z
jrs@laptop:~/sb/sb22/test$ scriba ranchr.sb
Q
jrs@laptop:~/sb/sb22/test$ scriba ranchr.sb
b
jrs@laptop:~/sb/sb22/test$ scriba ranchr.sb
3
jrs@laptop:~/sb/sb22/test$




« Last Edit: September 24, 2016, 05:58:58 PM by John »