Page 1 of 2

TI-BASIC program - Need suggestions.

Posted: Mon Apr 03, 2006 4:41 pm
by Patz QuickBASIC Creations
I'm not sure if TI-BASIC questions are allowed on the forum, but here it goes.

I recently made a program to generate random numbers that would never repeat.

Here is the dreadfully slow (not mine) version.

Notes: > specifies a STORE function, not GREATER THAN
<> specifies the TI-BASIC Is Not Equal To
These are both using minimal eye-candy
LGROUP and LGRPB are lists.
I can't use code tags...

Input A
{0}>LGROUP
0>D
0>F
While dim(LGROUP) <> A
int(rand*A)+1>B
For(C,1,dim(LGROUP)
IF B=LGROUP(C)
1>D
END
IF D <> 1
THEN
F+1>F
B>LGROUP(F)
END
Output(8,1,dim(LGROUP))
0>D
END
Output(1,1,LGROUP)



And my new (faster) version.


Input A
{0}>LGROUP
seq(B,B,1,A)>LGRPB
For(C,1,A)
int(rand*dim(LGRPB)+1>D
LGRPB(D)>LGROUP(C)
0>LGRPB
SortD(LGRPB)
dim(LGRPB)-1>dim(LGRPB)
End
DelVar LGRPB
Output(1,1,LGROUP)


Anyone have any tips for it? Speed or memory leak tips? (I already have muchos eye-candy, but didn't want to post it.) (if you can't use TI-BASIC then these programs are pretty useless)

Posted: Thu Apr 06, 2006 11:43 am
by Zim
This is really interesting. Could you explain a bit about what this code is supposed to do and how it does it? Also, why is your code better (faster?) than the other code. One question in particular, where does the value of B come from?

I do a little programming in TI-BASIC. I own a TI-83+ SE and my son owns a TI-84+ SE. We use TI-83+'s in the classroom and many of the students have their own calculator.

Re: TI-BASIC program - Need suggestions.

Posted: Thu Apr 06, 2006 4:13 pm
by Patz QuickBASIC Creations
A quick overview:
Both codes generate random numbers in a random order and store them in LGROUP. However, none of the numbers ever repeat.

Differences:
First code: Generates a random number, checks if it exists in the list already. If it doesn't, it adds the number to the list.
Second code: I'll do a walkthrough.

Input A
- Prompts for your maximum number
{0}>LGROUP
- Clears whatever may already be in the list already
seq(B,B,1,A)>LGRPB
-Generates a second list of numbers from 1 to A. (B is used as a counter.)
For(C,1,A)
- The ever wonderful FOR loop!
int(rand*dim(LGRPB)+1>D
-Generates a random location in the second list to use that number
LGRPB(D)>LGROUP(C)
-Stores the number in position C (used in the FOR loop)
0>LGRPB
-Replaces the value you just used with a 0
SortD(LGRPB)
- Sorts the list in descending order. (0 is put in the last spot)
dim(LGRPB)-1>dim(LGRPB)
-Remove the last number in the list. (Always going to be the 0)
End
-End the FOR loop.
DelVar LGRPB
-Delete the (blank) second list tom save RAM
Output(1,1,LGROUP)
- Shows the list.



The reason my code is faster is because it is guarenteed to never generate the same number, so it doesn't have to check if the number exists.
Quick $$$:
To group 28: Old program-30 seconds. New program-3 seconds.
To group 400: Old program-Close to 4 hours. New program-23 minutes.

The syntax of seq() (where the B is used) is as follows:

Code: Select all

seq(pattern,counter,start,end)
Pattern: The pattern to follow. An equation usually goes in this spot
Counter: The variable that changes.
Start: Where to start counting.
End: Where to end counting.

So, seq(B,B,1,A) generates a list that increases in size by 1 for every entry, until it reaches the number you gave it (where Input A is).


Too long of an explanation :-P If I'm unclear about something just tell me.

Posted: Fri Apr 07, 2006 12:34 pm
by Zim
Ok... so this could be useful for modeling what they call "Sampling Without Replacement" such as in drawing numbers (or names) out of a hat where you don't want the same number (name) called twice.

Very useful! I'll have to give it a look.

Posted: Fri Apr 07, 2006 4:16 pm
by Patz QuickBASIC Creations
That's what it was originally made for.

Posted: Thu Apr 13, 2006 4:49 pm
by Patz QuickBASIC Creations
In fact, that analogy is very close. Here is an example using it.

FIRST CODE:
1. Draws a random number.
2. Asks 'Have I used this number before?'
3. If not, add it to the list.
4. Put the number back in the hat. (every time)
5. Repeat.


SECOND CODE:
1. Get 2 hats. (One with numbers, one empty)
2. Draw a random number out of the first hat.
3. Records that number.
4. Puts it into the second hat. (so it will not be drawn again.)
5. Repeat.



Hopefully that shows you how my code is more efficient.
PATZ

Posted: Thu Apr 13, 2006 4:58 pm
by Patz QuickBASIC Creations
OK, I just finished a diagnostic on my new code. To group 800, the program takes: 10771 seconds (about 3 hours.) I have yet to try the old code with 800 as the maximum number, however I will start it and tell you the results on Monday. (if it's done by then :lol: )



*-just started-*

Posted: Mon Apr 17, 2006 5:52 pm
by Patz QuickBASIC Creations
To group 800 the old program took 32 hours. See how mine's better?

New: 3 hours
Old: 32 hours.

Posted: Sun May 07, 2006 7:54 pm
by Theophage
You young whippersnappers and your speedy code! Why in my day, we used code that let us come back a couple of days later to see if it was done or not, and we liked it!

Now, GET OFF MY LAWN!!!

Posted: Mon May 08, 2006 5:35 am
by Z!re
Theophage wrote:You young whippersnappers and your speedy code! Why in my day, we used code that let us come back a couple of days later to see if it was done or not, and we liked it!

Now, GET OFF MY LAWN!!!
haha :D

Posted: Wed May 10, 2006 12:18 pm
by Zim
In MY day, we'd turn in our stack of punch cards at the desk and come back in a couple of days to see if it had run.

Posted: Wed May 10, 2006 3:38 pm
by Patz QuickBASIC Creations
Theophage wrote:You young whippersnappers and your speedy code! Why in my day, we used code that let us come back a couple of days later to see if it was done or not, and we liked it!

Now, GET OFF MY LAWN!!!
Get into the 21st century. People don't keep track of who's on who's lawn... Oh, you mean the code...

Posted: Wed May 10, 2006 5:26 pm
by Deleter
Lawn? isnt that that flat expanse outside my house? What color was it again? Blue? I only go out at night as the sunlight is lethal...why do you make references to this odd thing in dealing with code, the ultimate source of logic?

:lol:

Posted: Thu May 11, 2006 4:10 am
by Theophage
Fear the Lawn!

It is the true and original source of the Almighty Grass Tile!

(besides, we're so off topic now, I'm sure we're on somebody's lawn...)

Re: TI-BASIC program - Need suggestions.

Posted: Mon Sep 11, 2006 4:53 pm
by Patz QuickBASIC Creations
I feel like an idiot.
An idiot once wrote:To group 800 the old program took 32 hours. See how mine's better?

New: 3 hours
Old: 32 hours
Newest: 80 seconds (thats roughly 1500x faster than the old, and 135x faster than the 'new'



NEW SOURCE: (It's also got a smaler file size! I feel sooo accomplished!)

Input A
{0}>LGROUP
seq(B,B,1,A)>LGRPB
For(C,1,A)
int(rand*dim(LGRPB)+1>D
LGRPB(D)>LGROUP(C)
LGRPB(dim(LGRPB))>LGRPB(D)
dim(LGRPB)-1>dim(LGRPB)
End
DelVar LGRPB
Output(1,1,LGROUP)


Well, it's faster. Anyone care to take a guess at the logic?

Well, I first wanted to speed it up more. So, I thought "Hey, the SortD( is the slowest part, right? Since all I was using it for was getting rid of a single number, I could just move the last number in the list and replace the number that had just been used with it. Now, it doesn't have to go through a sloooowwww... sorting proccess.

Posted: Mon Sep 11, 2006 7:48 pm
by {Nathan}
Funny timing. I just got a TI-84+ SE and am re-learning TI-BASIC, attempting to make a rather crappy text-game to be expanded later, but my RAM accidentally got cleared earlier today.

Posted: Tue Sep 12, 2006 11:08 am
by Zim
Using the software and cable that came with it, you can back up the whole thing to a computer and restore it later if you have to. I did this when I upgraded the operating system on my 83+SE.

Posted: Tue Sep 12, 2006 4:10 pm
by Patz QuickBASIC Creations
Although, for some odd reason, the TI-84 Plus Silver Edition doesn't run TI-BASIC files as well as the TI-84 Plus. (I have an 84P)

Posted: Wed Sep 13, 2006 2:25 pm
by {Nathan}
I also noticed that the scrolling on the APPS menu is MUCH slower than on the 83+/84+. It kinda annoys me when scrolling to NoteFolio to write down my homework, but the 1 megabyte of flash memory is worth it.

Posted: Wed Sep 13, 2006 5:04 pm
by Patz QuickBASIC Creations
Aight, here we go, 84+ n00b...
Nathan1993 wrote:I also noticed that the scrolling on the APPS menu is MUCH slower than on the 83+/84+.
2 solutions: Since the 84SE comes with a ton of apps, erase the ones you'll never use. Download OMNICALC from Detached Soultions and activate the Fast App Menu option. It's LIGHTNING fast!
http://detachedsolutions.com/omnicalc/
Nathan1993 wrote:It kinda annoys me when scrolling to NoteFolio to write down my homework
Press ALPHA+N to go straight to the apps that start with N.
Nathan1993 wrote:but the 1 megabyte of flash memory is worth it.
Pffffftt... I can use a flash drive on an 84+.
http://www.ticalc.org/archives/news/art ... 39572.html
(it's fairly new)