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

Random 10-digit generator

Post by Shaun_B »

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 4907 times
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Random 10-digit generator

Post by XavSnap »

: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
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
Shaun_B
Posts: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: Random 10-digit generator

Post by Shaun_B »

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: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Random 10-digit generator

Post by XavSnap »

Cool !
:D
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
dr beep
Posts: 2059
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Random 10-digit generator

Post by dr beep »

IMG_4887.PNG
(94.58 KiB) Downloaded 464 times
Shaun_B
Posts: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: Random 10-digit generator

Post by Shaun_B »

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: 5087
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Random 10-digit generator

Post by 1024MAK »

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

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
Post Reply