Random 10-digit generator

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Shaun_B
Posts: 379
Joined: Wed Apr 22, 2009 9:22 am

Random 10-digit generator

Attached is a listing that generates a random 10-digit ticket with each number from 0 to 9 inclusive in the sequence only once per run.

I wrote it to answer a code-golf question on Stack Exchange, with the ZX81 being an interesting beast as it's not very easy to golf on it (i.e., minimise and obfuscate the answers). I'm sure one of you guys could make it more golf-like. In fact, looking at it I could remove the GOSUB to increase the program speed a little; I could also change for FOR loop to could from 0 to 9.

Enjoy,

Shaun.
Attachments
zx81-ticket-generator.png (12.72 KiB) Viewed 515 times

XavSnap
Posts: 472
Joined: Sat May 10, 2008 3:23 pm

Re: Random 10-digit generator

May be faster! (don't use the recursive RND function! Only 10 loops)
1 LET A\$="0123456789"
2 LET B\$=""
3 LET R=INT(RND*LEN A\$)+1
5 LET B\$=B\$+A\$(R)
6 LET A\$=A\$( TO R-1)+A\$(R+1 TO)
7 IF A\$<>"" THEN GOTO 3
8 PRINT B\$
9 STOP

Shaun_B
Posts: 379
Joined: Wed Apr 22, 2009 9:22 am

Re: Random 10-digit generator

Thanks for the tip, my amended answer did credit you here.

The listing is as follows (forgive the GOTO abuse... actually, don't):

Code: Select all

``````1 LET A\$="0987654321"
2 LET R=INT (RND* LEN A\$)+1
3 PRINT A\$(R);
4 LET A\$=A\$( TO R-1)+A\$(R+1 TO )
5 GOTO 2+((A\$="")*4)
``````
Regards,

Shaun.

XavSnap
Posts: 472
Joined: Sat May 10, 2008 3:23 pm

Re: Random 10-digit generator

Cool !

dr beep
Posts: 1057
Joined: Thu Jun 16, 2011 7:35 am
Location: Boxmeer

Re: Random 10-digit generator

IMG_4887.PNG (94.58 KiB) Viewed 401 times

Shaun_B
Posts: 379
Joined: Wed Apr 22, 2009 9:22 am

Re: Random 10-digit generator

The problem with Code Golf on the Stack Exchange is that it expects answers to be least typing, rather than actual fewest bytes because modern programming simply counts the number of characters in your script as how much memory it's taking.

Regards,

Shaun.

1024MAK
Posts: 1822
Joined: Mon Sep 26, 2011 9:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Random 10-digit generator

So to the age old question: is PRINT one character (as stored internally), or five...

Mark

Who is online

Users browsing this forum: No registered users and 2 guests