Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
I made myself a random name generator , but I'm still new to programming. Is there any way I can make this shorter/faster using only Basic? Thanks in advance.
There's a one-liner for that, but with lots of padding using spaces, probably. Shorter still would be to separate names out with commas, then use a For() loop to read out what's between the Nth and (N+1)th commas.

Strings

For()

Go for it.
You might as well have a list of indices since there is a big gap between the lengths of the names. This means that padding them with spaces isn't going to be worth it.
What you can do is run the following:

Code:
{0→L₁
For(N,1,50
If N=1
Then
"James"→Str9
End
If N=2
Then
"David"→Str9
End
If N=3
Then
"Christopher"→Str9
End
If N=4
Then
"George"→Str9
End
If N=5
Then
"Ronald"→Str9
End
If N=6
Then
"John"→Str9
End
If N=7
Then
"Richard"→Str9
End
If N=8
Then
"Daniel"→Str9
End
If N=9
Then
"Kenneth"→Str9
End
If N=10
Then
"Anthony"→Str9
End
If N=11
Then
"Robert"→Str9
End
If N=12
Then
"Charles"→Str9
End
If N=13
Then
"Paul"→Str9
End
If N=14
Then
"Steven"→Str9
End
If N=15
Then
"Kevin"→Str9
End
If N=16
Then
"Michael"→Str9
End
If N=17
Then
"Joseph"→Str9
End
If N=18
Then
"Mark"→Str9
End
If N=19
Then
"Edward"→Str9
End
If N=20
Then
"Jason"→Str9
End
If N=21
Then
"William"→Str9
End
If N=22
Then
"Thomas"→Str9
End
If N=23
Then
"Donald"→Str9
End
If N=24
Then
"Brian"→Str9
End
If N=25
Then
"Jeff"→Str9
End
If N=26
Then
"Mary"→Str9
End
If N=27
Then
"Patricia"→Str9
End
If N=28
Then
"Linda"→Str9
End
If N=29
Then
"Barbara"→Str9
End
If N=30
Then
"Elizabeth"→Str9
End
If N=31
Then
"Jennifer"→Str9
End
If N=32
Then
"Maria"→Str9
End
If N=33
Then
"Susan"→Str9
End
If N=34
Then
"Margaret"→Str9
End
If N=35
Then
"Dorothy"→Str9
End
If N=36
Then
"Lisa"→Str9
End
If N=37
Then
"Nancy"→Str9
End
If N=38
Then
"Karen"→Str9
End
If N=39
Then
"Betty"→Str9
End
If N=40
Then
"Helen"→Str9
End
If N=41
Then
"Sandra"→Str9
End
If N=42
Then
"Donna"→Str9
End
If N=43
Then
"Carol"→Str9
End
If N=44
Then
"Ruth"→Str9
End
If N=45
Then
"Sharon"→Str9
End
If N=46
Then
"Michelle"→Str9
End
If N=47
Then
"Lauren"→Str9
End
If N=48
Then
"Sarah"→Str9
End
If N=49
Then
"Isabel"→Str9
End
If N=50
Then
"Ana"→Str9
End
length(Str9→L₁(1+dim(L₁
End
ΔList(cumSum(L₁→L₁
cumSum(L₁)-4→L₁
augment(L₁,{length(Str9)+L₁(dim(L₁→L1

This will return a list containing the indices for each name. Do the same with the last names.
You can then make the code:

Code:
{1,6,17,23,29,33,40,46,53,60,66,73,77,83,88,95,101,105,111,116,123,129,135,140,144,148,156,161,168,177,185,190,195,203,210,214,219,224,229,234,240,245,250,254,260,268,274,279,285,288,291→L₁
"DavidChristopherGeorgeRonaldJohnRichardDanielKennethAnthonyRobertCharlesPaulStevenKevinMichaelJosephMarkEdwardJasonWilliamThomasDonaldBrianJeffMaryPatriciaLindaBarbaraElizabethJenniferMariaSusanMargaretDorothyLisaNancyKarenBettyHelenSandraDonnaCarolRuthSharonMichelleLaurenSarahIsabelAna→Str1
randInt(1,50)->N
sub(Str1,L₁(N),L₁(N+1)-L₁(N)→Str2
{1,613,21,26,31,37,42,48,57,63,71,79,85,91,97,106,111,118,126,131,136,139,144,150,154,160→L₁
"SmithJohnsonWilliamsBrownJonesMillerDavisGarciaRodriguezWilsonMartinezAndersonTaylorThomasHernandezMooreJacksonThompsonWhiteLopezLeeClarkWalkerHallHarris→Str1
randInt(1,25)->N
Str2+" "+sub(Str1,L₁(N),L₁(N+1)-L₁(N)→Str2

You could definitely get better size by sorting the names by length, which would allow you to create the list with combinations of augment(), cumSum() and binomcdf() instead of just declaring it, but I didn't bother going that far, because the savings would probably be pretty small. You could also merge the two strings and lists and just add 50 to the index when looking for a last name which would save some more bytes.
1. The original code is inefficient on the micro level. The below code is basically the same but uses single-line If statements and the Ans variable and reduces size from ~2000 to ~1500 bytes:


Code:
randInt(1,50)->N
If N=1
"James
If N=2
"David
If N=3
"Christopher
...
Ans->Str9
...


2. Repeating yourself is inefficient and also bad programming practice. The solution by mrwompwomp is about 1200 bytes and blazing fast, while Weregoose's is about 1000 bytes, because code is repeated once or twice rather than 75 times. Using mrwompwomp's suggested improvements also gives a ~1000 byte solution.

3. Lowercase letters take 2 bytes to store whereas uppercase letters take 1. This means a long string of names like mrwompwomp's takes 815 bytes, leaving little space for the program. If you instead store all the strings in uppercase and convert to lowercase (except the first letter), you can get the string down to 445 bytes and the total length of code down to 677 bytes at a reasonable speed. The below snippets may be helpful in the case-conversion routine:

Xth character of a string Str9: sub(Str9,X,1)
Position of Str1 in the uppercase alphabet: inString("ABCDEFGHIJKLMNOPQRSTUVWXYZ",Str1)
Xth character of the lowercase alphabet: sub("abcdefghijklmnopqrstuvwxyz",X,1)
Append a character Str2 to a string Str9: use Str9+Str2
Still, doing the comma approach effectively caps the size of each index to 1, eliminates the need for lists, and makes insertion/removal of entries a bit more simple. If we're careful, we can put all of the first and last names into one string, then let randInt()'s arguments differentiate between the two. By the same license, we can tack our randomized name onto the end of the "parent" string, then sub() out the unwanted portion.

992 bytes (as prgmA):

Code:
:",James,David,Christopher,George,Ronald,John,Richard,Daniel,Kenneth,Anthony,Robert,Charles,Paul,Steven,Kevin,Michael,Joseph,Mark,Edward,Jason,William,Thomas,Donald,Brian,Jeff,Mary,Patricia,Linda,Barbara,Elizabeth,Jennifer,Maria,Susan,Margaret,Dorothy,Lisa,Nancy,Karen,Betty,Helen,Sandra,Donna,Carol,Ruth,Sharon,Michelle,Lauren,Sarah,Isabel,Ana,Smith,Johnson,Williams,Brown,Jones,Miller,Davis,Garcia,Rodriguez,Wilson,Martinez,Anderson,Taylor,Thomas,Hernandez,Moore,Jackson,Thompson,White,Lopez,Lee,Clark,Walker,Hall,Harris,
:For(A,0,1
:Ans→Str1
:1
:For(B,1,randInt(50A+1,25A+50
:Ans+1→C
:inString(Str1,",",Ans
:End
:Str1+" "+sub(Str1,C,Ans-C
:End
:sub(Ans,523,length(Ans)-522
That has advantages, but it does run in O(names^2). My code is fairly slow but runs in linear time; constructing the list a little more verbosely can make it somewhat faster. But some delay from converting to lowercase is unavoidable.


Code:
cumSum({1,7,15,13,8,4,1,0,1,1,1,8,7,2,4
3+seq(remainder(sum(X>Ans),9),X,1,76->L1
For(I,0,1
   randInt(75^I,50+I
   sub("ODRIGUEZRERNANDEZHILLIAMSWARTINEZMNDERSONAHOMPSONTOHNSONJACKSONJILLERMARCIAGILSONWAYLORTHOMASTALKERWARRISHMITHSROWNBONESJAVISDOOREMHITEWOPEZLLARKCALLHEELHRISTOPHERCLIZABETHEATRICIAPENNIFERJARGARETMICHELLEMICHARDRENNETHKNTHONYAHARLESCICHAELMILLIAMWARBARABOROTHYDEORGEGONALDRANIELDOBERTRTEVENSOSEPHJDWARDEHOMASTONALDDANDRASHARONSAURENLSABELIAMESJAVIDDEVINKASONJRIANBINDALARIAMUSANSANCYNARENKETTYBELENHONNADAROLCARAHSOHNJAULPARKMEFFJARYMISALUTHRNAA",sum(L1,1+Ans)-8,L1(Ans
   For(X,1,length(Ans)-1
         Ans+sub("abcdefghiklmnoprstuvwyz",inString("ABCDEFGHIKLMNOPRSTUVWYZ",sub(Ans,X,1)),1
   End
   sub(Ans,X,X
   If not(I:Ans+" ->Str9
End
Str9+Ans
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement