### Author Topic: Random Character  (Read 3431 times)

#### John

• Sr. Member
• Posts: 362
##### Random Character
« on: 24. September 2016, 10:20:48 »
Challenge:

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

#### Marcus

• Globaler Moderator
• Full Member
• Posts: 245
##### Re: Random Character
« Reply #1 on: 24. September 2016, 12:54:59 »
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) endifendfunc`
I'm guessing you have some ScriptBasic ace up your sleeve?

#### Mike Lobanovsky

• Full Member
• Posts: 162
##### Re: Random Character
« Reply #2 on: 24. September 2016, 14:59:15 »
IIF is the function:

Code: [Select]
`#AppType ConsoleRandomizePrint \$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.
Mike
____________________________________________________________________________________________
(3.6GHz i5-3470, 16GB RAM / 2 x nVidia GTX 650Ti SLI-bridged, 2GB VRAM / x64 Win 7, x86 elementaryOS Luna)

#### Marcus

• Globaler Moderator
• Full Member
• Posts: 245
##### Re: Random Character
« Reply #3 on: 24. September 2016, 15:21:25 »
C'mon, John, show us yours!

#### Tomaaz

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

#### Marcus

• Globaler Moderator
• Full Member
• Posts: 245
##### Re: Random Character
« Reply #5 on: 24. September 2016, 17:02:38 »
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: 24. September 2016, 17:05:15 by Marcus »

#### Mike Lobanovsky

• Full Member
• Posts: 162
##### Re: Random Character
« Reply #6 on: 24. September 2016, 17:26:28 »
Yeah, that one would have a much better distribution profile.

Cf.:

Code: [Select]
`#AppType ConsoleRandomizePrint "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...
Mike
____________________________________________________________________________________________
(3.6GHz i5-3470, 16GB RAM / 2 x nVidia GTX 650Ti SLI-bridged, 2GB VRAM / x64 Win 7, x86 elementaryOS Luna)

#### Tomaaz

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

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

• Full Member
• Posts: 162
##### Re: Random Character
« Reply #8 on: 24. September 2016, 17:44:28 »
Attempt #3:

Code: [Select]
`#AppType ConsoleRandomizePrint 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.
Mike
____________________________________________________________________________________________
(3.6GHz i5-3470, 16GB RAM / 2 x nVidia GTX 650Ti SLI-bridged, 2GB VRAM / x64 Win 7, x86 elementaryOS Luna)

#### Tomaaz

• Guest
##### Re: Random Character
« Reply #9 on: 24. September 2016, 18:01:24 »
Where is John???

#### Mike Lobanovsky

• Full Member
• Posts: 162
##### Re: Random Character
« Reply #10 on: 24. September 2016, 18:02:03 »
Sleeping. He's an U.S. citizen, after all...
Mike
____________________________________________________________________________________________
(3.6GHz i5-3470, 16GB RAM / 2 x nVidia GTX 650Ti SLI-bridged, 2GB VRAM / x64 Win 7, x86 elementaryOS Luna)

#### Marcus

• Globaler Moderator
• Full Member
• Posts: 245
##### Re: Random Character
« Reply #11 on: 24. September 2016, 18:10:25 »
Sleeping. He's an U.S. citizen, after all...

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

#### Mike Lobanovsky

• Full Member
• Posts: 162
##### Re: Random Character
« Reply #12 on: 24. September 2016, 18:12:38 »
Quote
Dammit, you can't just start a war and then disappear!

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

In the meantime, attempt #4:

Code: [Select]
`#AppType ConsoleRandomizePrint randchar()Print randchar()Print randchar()Print randchar()Print randchar()Print randchar()Print randchar()Print randchar()PauseFunction 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[].
Mike
____________________________________________________________________________________________
(3.6GHz i5-3470, 16GB RAM / 2 x nVidia GTX 650Ti SLI-bridged, 2GB VRAM / x64 Win 7, x86 elementaryOS Luna)

#### jj2007

• Full Member
• Posts: 124
• MasmBasic rocks
##### Re: Random Character
« Reply #13 on: 24. September 2016, 18:43:28 »
Print "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"{RandInt(1, 62)}

Nice but not basic enough, Mike

Init
For_ ct=0 To 999
Print Mid\$("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789", Rand(62)+1, 1)
Next
EndOfCode

#### John

• Sr. Member
• Posts: 362
##### Re: Random Character
« Reply #14 on: 24. September 2016, 18:44:44 »
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 IFEND FUNCTIONPRINT 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: 24. September 2016, 19:58:58 by John »