Author Topic: Random Character  (Read 16218 times)

B+

  • Guest
Re: Random Character
« Reply #30 on: September 25, 2016, 03:12:10 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. :)



If there is monkey business to be done, I am your primate!
Code: [Select]
' Act1.bas  SmallBASIC 0.12.6 [B+=MGA] 2016-09-25

def randChar() = mid("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", (rnd*62)\1 + 1, 1)

startHamlet = "Act1"
build = ""
while build <> startHamlet
  rCh = randChar
  trials ++
  if trials % 1000 = 999 then cls: print trials, build
  if rCh = mid(startHamlet, len(build) + 1, 1) then build = build + rCh else build = ""
wend
? "It took ";trials;" trials to build ";build;"."
pause

This code needs more speed for anything more ambitious, hint, hint...

B+

  • Guest
Re: Random Character
« Reply #31 on: September 25, 2016, 03:47:25 PM »
How many spaces have to be added to get a distribution of word lengths that matches Shakespeare's or general use?

Append: It is not going to work well that way.
« Last Edit: September 25, 2016, 04:33:09 PM by B+ »

ZXDunny

  • Guest
Re: Random Character
« Reply #32 on: September 25, 2016, 08:18:40 PM »
And ... what compatibility does SpecBAS claim, Paul? :)

I'd say I'm about 90 - 95% compatible with the original Sinclair BASIC, barring some insurmountable issues such as colour depth and memory layout. So far all the programs I've tested that were written for the original hardware work exactly as they should with some very minor modifications for aforesaid colour issues.

ScriptBasic

  • Guest
Re: Random Character
« Reply #33 on: September 25, 2016, 08:37:49 PM »
And ... what compatibility does SpecBAS claim, Paul? :)

I'd say I'm about 90 - 95% compatible with the original Sinclair BASIC, barring some insurmountable issues such as colour depth and memory layout. So far all the programs I've tested that were written for the original hardware work exactly as they should with some very minor modifications for aforesaid colour issues.

That's an amazing accomplishment Paul!

Is your belief that Sinclair BASIC should live forever as an example of its excellence or is this just a serious hobby that makes you happy? I'm not trying to be a jerk asking these questions. All I'm asking is what makes a retro BASIC programmer tick?




Tomaaz

  • Guest
Re: Random Character
« Reply #34 on: September 25, 2016, 08:46:05 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$

Looks more like Perl.  ;)
Code: [Select]
until ($a =~ /[^\W_]/) {$a = chr(int(rand(128)))} print $a


Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #35 on: September 25, 2016, 09:33:57 PM »
You mean "Who's there?"  ;D  Actually not too many to be infeasible. :)
This code needs more speed for anything more ambitious, hint, hint...

Actually I seem to have been too optimistic saying what I said. While getting "Who's " printed out is fairly straight forward (in C and case insensitive, of course, for speed reasons), "Who's t" hasn't yet been seen by me at all.

Code: [Select]
#AppType Console

hamlet()

Pause

DynC hamlet()
  #define randChar() "?' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"[rand() % 55]
  #define startHamlet "Who's " // "there?"
 
  void main()
  {
    char build[13];
    int rCh, pos = 0, trials = 0;
   
    memset(build, 0, 13);
    srand(time());
   
    while (_stricmp(build, startHamlet) && ++trials) {
      rCh = randChar();
      if (toupper(rCh) == toupper(startHamlet[pos]))
        build[pos++] = rCh;
      else {
        memset(build, 0, pos);
        pos = 0;
      }
    }
    printf("It took %d trials to build '%s'.\n", trials, build);
  }
End DynC

B+

  • Guest
Re: Random Character
« Reply #36 on: September 25, 2016, 10:14:18 PM »
Hi Mike,

I am curious, dropping the numbers and loosing the case sensitivity and I assume you are inserting the apostrophe, so

26 ^ (number of letters to match)

26 ^ 5 ~ 12 million +/- random luck, should get to whost faster???


Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #37 on: September 25, 2016, 10:37:03 PM »
Hi Mike,

I am curious, dropping the numbers and loosing the case sensitivity and I assume you are inserting the apostrophe, so

26 ^ (number of letters to match)

26 ^ 5 ~ 12 million +/- random luck, should get to whost faster???

Hehe,

Using #define randChar() "?' ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand() % 29] and a lot of random luck (close to 1.9G trials), I'm finally seeing the attached.

Guess I have to go to unsigned long long's to count the trials for "Who's th". :D
« Last Edit: September 25, 2016, 11:11:17 PM by Mike Lobanovsky »

ZXDunny

  • Guest
Re: Random Character
« Reply #38 on: September 25, 2016, 10:46:57 PM »
Is your belief that Sinclair BASIC should live forever as an example of its excellence or is this just a serious hobby that makes you happy? I'm not trying to be a jerk asking these questions. All I'm asking is what makes a retro BASIC programmer tick?

A little from column A, a little from column B.

Seriously though, Sinclair BASIC wasn't that amazing - it was slower than BBC BASIC (though quicker than C64) and the underlying hardware meant that the graphics were quite restricted. What made it a joy to code was things like its string slicing, which was incredibly flexible without using up tokens for LEFT$/RIGHT$/MID$ - or any tokens at all for that matter - and easy access to user defined character sets/graphics. To do anything interesting the BBC user had to delve into VDU commands which was in no way intuitive or accessible. Even the enormous flexibility of the FOR .. NEXT system was nowhere to be seen in other implementations - you could set up a loop and have multiple NEXTs for that same loop...

But as for me being a "retro BASIC programmer?" Well, yes, that I am - despite being a professional programmer, I do love BASIC. I started with it when I was very, very young and no matter what I did it was never fast enough; sprites and speedy graphics or sound were exclusively the domain of the assembly programmer. Of course, new faster CPUs were always on the horizon and I envisaged a day when the CPU would be fast enough that I could program in BASIC producing games and applications at the level of MC back then.

Of course, as CPUs increased in power so did the applications we demanded of them and that never happened. I moved on from BASIC to Pascal, C, Asm in various flavours etc. And one day I decided that I would write a BASIC interpreter that brought my favourite dialect up to scratch.

Hence, I wrote a z80 emulator - which when paired with the right ROM Image ran the BASIC. After that I added hooks into that ROM image to allow an editor to pass BASIC code in and out. Then I cranked the speed up, but the graphics were lacking (256x192 at 1 BPP with overlaid attribute colours). So after that, I wrote SpecBAS which fulfills all my needs.

I'd never written an interpreter before, so it was interesting to see how others solved the problems they came up against and compare to mine. I enjoy (as you already know) taking programs in other dialects and seeing if SpecBAS can achieve the same things.

So yes, it's a hobby. My paid work is in very low level languages and rarely goes up above C or Pascal level, but what takes me several hundred lines in those I can do in about three in SpecBAS, and that's where I get my fun.
« Last Edit: September 25, 2016, 10:49:00 PM by ZXDunny »

ScriptBasic

  • Guest
Re: Random Character
« Reply #39 on: September 25, 2016, 10:58:10 PM »
Quote
Looks more like Perl.  ;)
Code: [Select]
until ($a =~ /[^\W_]/) {$a = chr(int(rand(128)))} print $a

But it doesn't meet the criteria of the challenge.

Code: [Select]
DECLARE SUB pl_Init ALIAS "pl_Init" LIB "sbperl"
DECLARE SUB pl_Eval ALIAS "pl_Eval" LIB "sbperl"
DECLARE SUB pl_Destroy ALIAS "pl_Destroy" LIB "sbperl"

pl_Init
pl_code = """
until ($a =~ /[^\W_]/) {$a = chr(int(rand(128)))} print $a,'\n'
"""
pl_Eval pl_code
pl_Destroy


jrs@laptop:~/sb/sb22/perl$ scriba tomaaz_perl.sb

jrs@laptop:~/sb/sb22/perl$ scriba tomaaz_perl.sb
"
jrs@laptop:~/sb/sb22/perl$ scriba tomaaz_perl.sb
~
jrs@laptop:~/sb/sb22/perl$ scriba tomaaz_perl.sb
p
jrs@laptop:~/sb/sb22/perl$




Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #40 on: September 25, 2016, 11:53:32 PM »
Nice. This is my BASIC version.

Attempt #6

Code: [Select]
#APPTYPE CONSOLE
#OPTION IMPLICIT

RANDOMIZE
REPEAT 8
  DO
    ch = CHR(RANDINT(1, 255))
  LOOP UNTIL REGEXEC(ch, "[a-zA-Z0-9]")
  PRINT ch;
END REPEAT
PAUSE

Typical output:

EmHOo74t
Press any key to continue...


:)
« Last Edit: September 26, 2016, 12:08:40 AM by Mike Lobanovsky »

Mike Lobanovsky

  • Guest
Re: Random Character
« Reply #41 on: September 26, 2016, 12:38:42 AM »
I am curious ... ~ 12 million +/- random luck, should get to whost faster???

Ideally yes but seriously, those simplistic randomizers we're using are very weak indeed. For example, my

#define randChar() "?' ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand() % 29]

uses C standard rand() that generates 32K integers in the range of 0 <= n < RAND_MAX with more or less uniform distribution of n across the 32K integer range. But as soon as we start chomping its output in order to get the limits we'd like, we would in fact be distorting its distribution dramatically. In other words, we would inevitably elevate the risk of generating a sequence of repeated, rather than random, characters at our randChar()'s output, which would consequently nullify most of our builds because the original "Who's there?" has no repeated characters at all.

That's why I said we can play with all sorts of rnd()/rand()/randint() in our BASIC sandbox just for fun as much as we like but, if we want to do anything serious, we should resort to specialized 3rd party libraries. But are all BASICs present here equipped well enough to face this kind of a challenge? ;)

ScriptBasic

  • Guest
Re: Random Character
« Reply #42 on: September 26, 2016, 03:59:46 AM »
I'm happy with the RANDOMIZE / RND functions in Script BASIC if properly seeded. I think you can get a good visual of the random effect by looking at the One Line Graphic post I made. (see attached image) The images were generated in the same way with one full intensity alpha channel and the attached image randomizes the alpha channel.  Another example of SB randomness is THIS example. (done in 3 hundredths of a second on my old PoS laptop)
« Last Edit: September 26, 2016, 04:45:43 AM by John »

B+

  • Guest
Re: Random Character
« Reply #43 on: September 26, 2016, 06:32:01 AM »
Mike, I think you hit the lottery! 29 ^ 7 matches is 11 digits long, your trials are only 10 digits long.

Can not random numbers be extended by using random numbers to fill digit spots.
digit 1 is random number
digit 2 is random number
...
.
.
.

Tomaaz

  • Guest
Re: Random Character
« Reply #44 on: September 26, 2016, 07:13:11 AM »
Quote
Looks more like Perl.  ;)
Code: [Select]
until ($a =~ /[^\W_]/) {$a = chr(int(rand(128)))} print $a

But it doesn't meet the criteria of the challenge.


It does when it's run in Perl. I don't know about your Perl extension for SB. It looks like regular expressions don't work with it. Also, '\n' will not print a newline in Perl. You need to use double quotes "\n" (for example print "$a\n"). So, there is something strange about the way your extension work.