TI-89 HomeGames ArchiveTitanium GamesGames How ToProgramsFAQForumTI-89 WikiLinks

Probability and statistics

 
Post new topic   Reply to topic    Your TI-89 Resource Forum Index -> General Discussion
Author Message
pickaname



Joined: 21 Mar 2007
Posts: 2

PostPosted: March 21, 2007 7:04 PM    Post subject: Probability and statistics Reply with quote

I am assuming this calculator is able to simulate rolling dice and drawing cards but I cannot find it
Back to top
View user's profile Send private message
bluesuit



Joined: 08 Nov 2006
Posts: 42

PostPosted: April 29, 2007 3:44 PM    Post subject: Reply with quote

For rolling dice:

enter:

rand(x)

and it will return a value between 1 and x, inclusive

For drawing cards
I am not aware of any functions for a card deck simulation, however, I think it wouldn't be hard to make. You would have to make a list with 52 string values for each card in the deck. For example
Code:
{"1 of diamonds", "1 of spades", "1 of hearts", "1 of clubs", "2 of diamonds", "2 of spades", "2 of hearts", "2 of clubs", .... , "King of diamonds", "King of spades", "King of hearts", "King of clubs"} -> cards
then in a program
Code:
disp cards[rand(52)]
Back to top
View user's profile Send private message Visit poster's website
mellamokb



Joined: 10 May 2007
Posts: 124

PostPosted: May 17, 2007 3:09 PM    Post subject: Reply with quote

Hi,

This would probably be a little easier to program.

Code:

{"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"}->cardrank
{"Clubs","Diamonds","Hearts","Spades"}->cardsuit

cardrank[rand(13)]&" of "&cardsuit[rand(4)]->card
disp card



~ mellamokb
Back to top
View user's profile Send private message
bluesuit



Joined: 08 Nov 2006
Posts: 42

PostPosted: May 17, 2007 3:56 PM    Post subject: Reply with quote

I think the code you posted might work but it would be harder to control how many times a particular card came up. for example, you couldn't keep a 2 of spades from coming up as easy in a game of solitaire.
Back to top
View user's profile Send private message Visit poster's website
mellamokb



Joined: 10 May 2007
Posts: 124

PostPosted: May 17, 2007 8:52 PM    Post subject: Reply with quote

Hi bluesuit,

I think you're right. You would somehow need a list of 52 somethings to keep track of which cards have already been selected. I might suggest a compromise: use my method to generate the list of the 52 cards instead of typing them in by hand, and then use your idea to simplify tracking which cards have already been used, like this:

Code:

{"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"}->cardrank
{"clubs","diamonds","hearts","spades"}->cardsuit
seq(seq(cardrank[i]&" of "&cardsuit[j],i,1,13),j,1,4)->cardmat
mat»list(cardmat)->cardlist



Cardlist would then contain the 52 cards spelled out as you posted. Interesting problem...I wonder if it would be even more efficient yet to use the numbers 1-52 to represent the 52 cards. It would take more programming, but when I did it on my calculator the list containing 1-52 was only 160 bytes compared to 797 bytes for the complete card list.

Code:

seq(x,x,1,52)->cardnum



Here's a function that would input a number 1-52 and output the card corresponding to that number:

Code:

getcard(num)
Func
Local rank, suit, num0
num-1->num0
Mod(num0,13)+1->rank
Int(num0/13)+1->suit
Return cardrank[rank]&" of "&cardsuit[suit]
EndFunc



Note: I use num0 to represent the 0-based index version of num, since the math I am using here to determine the rank and suit rely on 0-based indexing.

You would just have to manage the list of numbers, taking out the cards that are used from the list. Then to determine what is on a randomly selected card, run it through the getcard() function. It would be simple to write getrank() and getsuit() functions that simply return the rank (1-13) and suit (1-4) for testing valid solitaire moves, for instance.

Code:

getrank(num)
Func
Local num0
num-1->num0
Return Mod(num0,13)+1
EndFunc



Code:

getsuit(num)
Func
Local num0
num-1->num0
Return Int(num0/13)+1
EndFunc



Fun problem!

~ mellamokb
Back to top
View user's profile Send private message
bluesuit



Joined: 08 Nov 2006
Posts: 42

PostPosted: May 17, 2007 9:26 PM    Post subject: Reply with quote

Wow, it's cool that people actually respond quickly at the forum. This seems kind of like a dead site.

Yes, the code seems like it would work, but is the function that reads the number sequence along with the number sequence generator actually smaller than just creating a list with all the cards?

Bluesuit

--
I have photographic memory, I just ran out of film
Back to top
View user's profile Send private message Visit poster's website
mellamokb



Joined: 10 May 2007
Posts: 124

PostPosted: May 17, 2007 9:39 PM    Post subject: Reply with quote

Well, let's liven things up a bit! I'm willing to spend a little spare time to answer questions, but there needs to be users to ask them Wink.

I entered all of the functions, programs, lists, etc. into my calculator, and these are the file sizes:

cardlist: 797 bytes
cardnum: 160 bytes
cardmat: 805 bytes (this can be deleted after the first piece of code is run)
cardrank: 69 bytes
cardsuit: 41 bytes
getcard: 116 bytes
getrank: 56 bytes
getsuit: 64 bytes

Using cardlist needs a total of 797 bytes--and there doesn't seem to be an easy way to determine the rank and suit of a specific card for a game without using the index of the card in the list and applying my cardrank and cardsuit functions anyway.

Using cardnum needs a total of 160 (cardnum) + 69 (cardrank) + 41 (cardsuit) + 116 (getcard) + 56 (getrank) + 64 (getsuit) bytes for a total of 506 bytes. Your idea is easier to conceptualize for a beginner programmer. Mine may be smaller but it is more advanced and less clear, and I could probably shave off even more bytes if I didn't write such redundant code and use such long variables for clarity.

I know I'll have some spare time in my first two classes tomorrow, so maybe I'll try to write a simple card game and post it. Hmm...should I do FreeCell, Solitaire, Spider Solitaire, Golf, Pyramids, Hearts, or Black Jack? I think I'll try Solitaire, it'll probably be the simplest. But it'll be slow in BASIC.

~ mellamokb
Back to top
View user's profile Send private message
bluesuit



Joined: 08 Nov 2006
Posts: 42

PostPosted: May 17, 2007 10:55 PM    Post subject: Reply with quote

When I was first reading your post, I didn't realize that the code you posted was a function. I had been thinking in terms of a BASIC program.

I've been reading some of your posts. Before this, I hadn't realized that it was possible to define functions. Cool.
_________________
~bluesuit


"I have photographic memory, I just ran out of film"
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:    View previous topic : View next topic  
Post new topic   Reply to topic    Your TI-89 Resource Forum Index -> General Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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

Contact Us | Privacy Policy | Disclaimer | Trademarks

Copyright ©2006 All rights reserved.


Free Greeting Cards | Girly Myspace Layouts | Myspace Unblocker |