Author Topic: Random Character  (Read 16238 times)

B+

  • Guest
Re: Random Character
« Reply #15 on: September 24, 2016, 04:45:54 PM »
Oh Basic!
Code: [Select]
def randChar() = mid("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", (rnd*62)\1 + 1, 1)
for i in seq(1, 1000, 1000) do ? randChar; " ";

Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #16 on: September 24, 2016, 04:56:13 PM »
Code: [Select]
FUNCTION RandomCharacter
  RANDOMIZE(NOW)
........

You'd better take RANDOMIZE out of your function, John. It's an extremely slow routine and it should be done only once at app start. Then all the random function flavors will only have to fetch the next number from the (typically) 32K long sequence that RANDOMIZE pre-generated, cycling to the beginning of it when its end is reached (which actually turns the entire setup to pseudo random but still usable for most purposes).
« Last Edit: September 26, 2016, 12:01:27 AM by Mike Lobanovsky »

Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #17 on: September 24, 2016, 05:09:59 PM »
Oh Basic!
Nice but not basic enough, Mike ;)

You want basics? Well, I've got some. Attempt #5:

Code: [Select]
#AppType Console
Macro randChar() = Mid("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", (Rnd() * 62) \ 1 + 1, 1)

Randomize
For Dim i = 1 To 1000: ? randChar(), " ";: Next

Pause

Typical output:

9 K z y u k f h F 5 e a 1 5 L p c P d V o 8 K D i M H m K F h h 7 f Q G n i H W w J o S H X Q D 4 u
X r a S 4 D M F e F Y n w G G f l 0 r D e r e 9 n m q 8 g l v j 8 F l o B H f u n o F 8 A y F l N 4
7 H 2 3 6 v f Z b K 5 Y V F l q 5 P x Z n R p S j G o W D 7 I r M X L Q E m W E p b s G r i w x s v
A q 0 N o y P V m 9 L Y q e g 1 G 5 B d 9 M u l A S T P T S 5 n z I e e e l y T 0 I 1 K B 2 l F F R
S S Z i h S l L P e 5 O r t n g O q D 9 i g V u 2 7 L w i K E u B D k E 3 1 5 i u X u H 1 3 u O J 5
G u m m j 5 E d H h O m C z 7 4 o h y D M I N g m Z I X X m F N e M W C U O p w 6 w w F a Y r f J b
Y M s 0 U R L x q H n 7 x 0 x v J E 3 k z o q n x t Z O j 4 I Q u B g V b P 4 Q X T t 8 S 6 7 G Q c
D w 5 D v z T J N 1 G l 4 f H q R 1 F 5 w O C l 1 9 w Q Q P T 8 G x 5 U t h C z U I b h j X a H z 3
E 3 H p 8 X o e W k p r R Y e i X J h e b H K Q E s j u o u e A w s X C G k w a k D S A N S X w K 4
e b t y L 6 9 k g S l 3 h r Z q 0 X U 6 V z 7 a 3 m 5 E z D d f Y R S A w r r N F e V f I H U t n p
U b c K S J D g a e k m k w r x d 1 b 4 t 7 U 6 x A F J S 3 X v K v v k p 2 D t R c G F g X V 1 f D
q 0 o q 7 B u d f 8 B s W M S e C E L c p x u N 3 l o A Z 6 c S z Z z K P m z r g h C p Q j i 6 k n
D a 5 A M l Z S i O 8 y h P r 2 4 3 l Y Y G u R 4 W v D o x 5 U p m c h X o 6 A f o B h 5 H 2 W 9 a
R C V v m M R 6 6 T w 4 l q f 1 4 g G 2 z X d y r y I U Q K N f P X y F P b y e b F 0 T H P s N P c
y 7 F Q V j D i d p u N F s 4 E V Z Z 3 r A 2 y 7 Q L Q 3 g t i F 2 g P e F q k 5 o 7 C 9 f g p 6 s
s z 1 m Q d 4 T K W N k h J w k g e e q l 7 K B 1 6 k 2 l r 4 e Y e Z 2 5 h w C u k 5 l q K z J G H
I m P v 6 0 w n t 3 D W O A j h J 6 6 r B N l 1 3 x Q y h 6 6 3 c B C q w 9 3 M 4 w f b b q Q J m Q
1 m T O g g 3 u R U v p c u n a g S N g A Y Q s 9 W 3 k 0 Q E w k f r H h 6 8 f z 4 5 M F S E X F e
b b d S 7 P 7 X K s R 9 E d k N c C w O 1 E p P i Z T V 8 L w 1 p V 4 9 4 W f u 1 G t Z 1 r 9 L 8 U
8 t a 1 a u p W c 2 7 j S c C 4 S K W 3 v U 5 u z T J y K T C t q p e 0 t k y H B p 9 F D i X s s m



P.S. Don't forget to RANDOMIZE, guys, else everywhere I write "Typical" you'll have to write "The only". ;)

Mopz

  • Guest
Re: Random Character
« Reply #18 on: September 24, 2016, 05:15:37 PM »
Just using Tomaz way here, but in naalaa:

Code: [Select]
randomize time()

for y = 1 to 16
for x = 1 to 16
write RandomChar(), " "
next
wln
next

wait keydown

function RandomChar$()
return mid("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", rnd(62))
endfunc

Next assignment: Write a computer chess player with as few line of codes as possible ;)  Actually that'd be kind of fun. I wonder if writing a small PROLOG interpreter would do the trick in a nice and time consuming way?
« Last Edit: September 24, 2016, 05:25:49 PM by Marcus »

B+

  • Guest
Re: Random Character
« Reply #19 on: September 24, 2016, 06:45:52 PM »
Quote
P.S. Don't forget to RANDOMIZE, guys, else everywhere I write "Typical" you'll have to write "The only". ;)

Not correct, I get different outputs without Randomize with SmallBASIC and in Just Basic docs, it says to use RANDOMIZE to get predictable/repeatable runs, eg for debugging. I can show more and different outputs without Randomize.

Append: Confirmed on SmallBASIC, when I Randomize with constant, I get repeated results.

On another front (maybe too philosophical):
If a chess player makes random but legal moves, is it really a chess player?
« Last Edit: September 24, 2016, 07:05:36 PM by B+ »

Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #20 on: September 24, 2016, 08:28:04 PM »
Not correct, I get different outputs without Randomize with SmallBASIC and in Just Basic docs, it says to use RANDOMIZE to get predictable/repeatable runs, eg for debugging. I can show more and different outputs without Randomize.

Sigh...

What's the use of RANDIMIZE at all if, as you say, your random functions yield different numbers every time you run them even as they are? ;) Go figure... Or ask the developers of those languages...

The C language and C-based BASICs would yield the same pseudo random sequence without RANDOMIZE because the seed for the random functions would remain the same. In other words, RANDOMIZE randomizes the seed and regenerates the sequence. Of course if RANDOMIZE uses the same constant argument, the new sequences will also be constant, albeit different from when used without an argument. RANDOMIZE TIMER is redundant in FBSL BASIC because that's what its RANDOMIZE would do on default -- much more often than randomizing with a constant argument.

So what exactly is incorrect in my logic?
« Last Edit: September 24, 2016, 08:30:00 PM by Mike Lobanovsky »

ScriptBasic

  • Guest
Re: Random Character
« Reply #21 on: September 24, 2016, 08:43:57 PM »
Quote
What's the use of RANDIMIZE at all if,

I used NOW as the seed for this example as the challenge was to return a single random character. If I were to generate a list of random characters, 1 second seeding isn't sufficient. I use the RANDOMIZE(gfx::Time()) SDL_gfx extension module function for millisecond resolution of randomness.


« Last Edit: September 24, 2016, 08:50:24 PM by John »

Tomaaz

  • Guest
Re: Random Character
« Reply #22 on: September 24, 2016, 09:00:16 PM »
Perl. Using regular expressions, of course. ;)

Code: [Select]
sub randchar() {
until ($a =~ /[A-Za-z0-9]/) {
$a = chr(int(rand(128)));
}
return $a;
}

Or something even more Perlish. ;)

Code: [Select]
sub randchar() {
until ($a =~ /[^\W_]/) {$a = chr(int(rand(128)))}
return $a;
}

Or...

Code: [Select]
sub randchar() {until ($a =~ /[^\W_]/) {$a = chr(int(rand(128)))} return $a}

 ;D
« Last Edit: September 24, 2016, 09:08:09 PM by Tomaaz »

ZXDunny

  • Guest
Re: Random Character
« Reply #23 on: September 24, 2016, 09:59:16 PM »

Sigh...

What's the use of RANDIMIZE at all if, as you say, your random functions yield different numbers every time you run them even as they are? ;) Go figure... Or ask the developers of those languages...

Well, that's how SpecBAS does it - the random seed at startup is determined by system time in ms. This way every time a program is run, you get random numbers, not a sequence. If you want a sequence, you use the RANDOMIZE keyword - with no parameter it sets the seed to the timer. With a numeric parameter, it uses that as the seed.

That's kinda the point, no?

B+

  • Guest
Re: Random Character
« Reply #24 on: September 24, 2016, 10:55:55 PM »
Not nearly as elegant as Tomaaz method but to add to rainbow of different ways this could be done:

Code: [Select]
dim c()
for i in seq(48, 57, 10) do append c, i
for i in seq(65, 90, 26) do append c, i
for i in seq(97, 122, 26) do append c, i
 
def rndChr = chr( c( (rnd * 62)\1 ) )

for i in seq(1, 1000, 1000) do ? rndChr; " ";

« Last Edit: September 24, 2016, 11:00:22 PM by B+ »

jj2007

  • Guest
Re: Random Character
« Reply #25 on: September 24, 2016, 11:23:17 PM »
an extremely slow routine

Ah, speed, the magic word... 8)

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  NumBytes=100000000
  Let edi=New$(NumBytes)
  Let esi="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"
  PrintCpu 0
  Rand()                  ; RANDOMIZE using rdtsc
  NanoTimer()
  For_ ecx=0 To NumBytes-1
      mrm [edi+ecx], [esi+Rand(62)]
  Next
  Print Str$("Generating %i random chars took", ecx), Str$(" %i ms", NanoTimer(ms))
  FileWrite "Random.txt", edi
  Inkey " - see the result (y)?"
  If_ eax=="y" Then ShEx "Random.txt"
EndOfCode


Output:
Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
Generating 100000000 random chars took 364 ms - see the result?

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: Random Character
« Reply #26 on: September 25, 2016, 08:43:25 AM »
CMLua (more the BASIC than the Lua way  ;)):
Code: [Select]
-- random char
function randomchar()
 char = mid ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",int (rnd()*62)+1,1)
 return char
end

cls()
for y=0,25, 2 do
 for x=0,64,2 do
    locate (x,y,randomchar())
  end
end

Hm, if we omit the numbers, what do you think how many trials are needed to get the beginning of Shakespeare's Hamlet ... ?
« Last Edit: September 25, 2016, 09:05:50 AM by Cybermonkey »

Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #27 on: September 25, 2016, 12:29:33 PM »
Hm, if we omit the numbers, what do you think how many trials are needed to get the beginning of Shakespeare's Hamlet ... ?

You mean "Who's there?"  ;D  Actually not too many to be infeasible. :)


Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #28 on: September 25, 2016, 01:30:19 PM »
... If you want a sequence, you use the RANDOMIZE keyword - with no parameter it sets the seed to the timer. With a numeric parameter, it uses that as the seed.

That's kinda the point, no?

Yes, it is. That's what FBSL would do too:

... if RANDOMIZE uses the same constant argument, the new sequences will also be constant, albeit different from when used without an argument. RANDOMIZE TIMER is redundant in FBSL BASIC because that's what its RANDOMIZE would do on default -- much more often than randomizing with a constant argument.

Well, that's how SpecBAS does it - the random seed at startup is determined by system time in ms. This way every time a program is run, you get random numbers, not a sequence...

That's kinda the point, no?

No, at least not to my point. FBSL i) incorporates an own ANSI C module, and ii) is designed to extensively use 3rd party DLLs usually written in C and linked against msvcrt.dll (system standard as opposed to crtdll.dll which isn't). FBSL BASIC calls to RANDOMIZE, RANDINT and RND are guaranteed to generate the exact same results as its own DynC and 3rd party calls to srand() and rand() at all times for 100% compatibility reasons.

How much do we want our data refined for us?

And ... what compatibility does SpecBAS claim, Paul? :)

........
Ah, speed, the magic word... 8)
........
  Rand()                  ; RANDOMIZE using rdtsc
........

What will you do with your Rand() under Windows 98 on a 486 chip, Jochen? FBSL runs on and under everything that's at least Windows 95 RC2 (with socket support) and has 32MB of RAM. :)



I used NOW as the seed for this example as the challenge was to return a single random character. If I were to generate a list of random characters, 1 second seeding isn't sufficient. I use the RANDOMIZE(gfx::Time()) SDL_gfx extension module function for millisecond resolution of randomness.

In fact, all these minutiae are extremely platform dependent, John. For any serious work with random numbers like e.g. cryptography, a specialized 3rd party library would be highly advisable.
« Last Edit: September 25, 2016, 10:15:04 PM by Mike Lobanovsky »

Peter

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

Nice. This is my BASIC version.

Code: [Select]
REPEAT
    ch$ = CHR$(RANDOM(256))
UNTIL REGEX(ch$, "[[:alnum:]]")
PRINT ch$