Random 10-digit generator

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

Random 10-digit generator

Post by Shaun_B » Thu Feb 16, 2017 4:19 pm

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
zx81-ticket-generator.png (12.72 KiB) Viewed 397 times

User avatar
XavSnap
Posts: 461
Joined: Sat May 10, 2008 3:23 pm

Re: Random 10-digit generator

Post by XavSnap » Thu Feb 16, 2017 7:24 pm

:D

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: 312
Joined: Wed Apr 22, 2009 9:22 am

Re: Random 10-digit generator

Post by Shaun_B » Fri Feb 17, 2017 9:42 pm

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.

User avatar
XavSnap
Posts: 461
Joined: Sat May 10, 2008 3:23 pm

Re: Random 10-digit generator

Post by XavSnap » Fri Feb 17, 2017 10:34 pm

Cool !
:D

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

Re: Random 10-digit generator

Post by dr beep » Tue Jul 11, 2017 8:54 pm

IMG_4887.PNG
IMG_4887.PNG (94.58 KiB) Viewed 283 times

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

Re: Random 10-digit generator

Post by Shaun_B » Tue Jul 18, 2017 8:52 am

Great answer Dr Beep.

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.

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

Re: Random 10-digit generator

Post by 1024MAK » Tue Jul 18, 2017 4:24 pm

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

Mark

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests