Random 1 to 10 question

newplumber

Senior Member
Hi everyone
I'm a expert/horrible player at the simple card game of Phase 10
and since I seem to play it more then I should I get sick of the normal 1-10 levels
so decided to place 1 thru 10 numbers (pieces of paper) in a hat and pick which level is next but
me being lazy thought hey a picaxe can do this somehow ?? so in short I was thinking
using a 2X16 LCD which is easy but were I became confused is to do a random number
1 thru 10 but make sure it doesn't use the same number
so if it was to pick 7 then it wouldn't pick 7 again till all the numbers are picked
its probably real easy to do ...but my brain just keeps getting random solutions :) that of course wont work
anyway hope some of you good poeples have a thought
I would use the 20m2 ....cause its cool
your friend who pretends to work
mark
 

premelec

Senior Member
You can use BIT variables as flags - e.g. when 7 needs to be excluded BIT7 = 1 ; if BIT7 THEN ... ELSE . to start again B0 = 0 and B1 = 0;
I'll leave it to you to do the detail work... ;-0 5 DegF 8" snow... going to bed...
 

hippy

Technical Support
Staff member
Here's the real world version I would use. Take 10 cards, write 1-10 on them, place them in a row ordered 1-10 from the left. Randomly select 1-10 and pick that Nth card from left.

Now nudge the cards to the left so it's a one card shorter row. Randomly select 1-9 and use that Nth card from left.

Repeat.
 

roho

Member
Hi newplumber,

I think you have a problem with your basic requirements: if you pick 7 first and then exclude that number until all the others have been picked, then you no longer have a random sequence. I think, in fact, that you have a fixed sequence. A better constraint would be not to select the same level twice running. This also has the advantage that it is easy to implement. The generic macro for generating a random number and assigning it to a variable is
Code:
#MACRO CalcVal(Reg, MinVal, MaxVal)
  LET Temp0 = MaxVal - MinVal + 1
  RANDOM RandNbr
  LET Reg = RandNbr // Temp0 + MinVal
#ENDMACRO
where RandNbr must be a word variable.
 

inglewoodpete

Senior Member
Another thing to do to get a "more random" number is to have the Random command repeatedly being executed on the chosen word variable in the main loop. When a button is pressed, a copy of the number is taken and used (roho's example above is a good one) for your processing and display. Once displayed, the main loop returns to repeating the Random command.

Maybe you should just pray for spring to come and the snow to stop falling!
 

newplumber

Senior Member
@ premelec thanks I will try to something like you said ...its - 8 f here and stopped snowing ...holding my breath

@ hippy thanks that is a cool idea ...seems the best for random number

@roho I will need more time to understand your code (i never did win a understand code race)
but thanks

@inglewoodpete thanks so the random # would be a fairly true random number with a button pressed as the random is always repeated
which makes sense to me ....btw yes we need spring ....or a picaxe driven snowblower :)... with the wind and trying to blow snow here ...almost better off sticking your head into the ski hill snow machines :) ...creates same results
 

hippy

Technical Support
Staff member
Here's my implementation of the algorithm in Post #3. Runs on real chip or can be simulated -

Code:
#Picaxe 20M2
#Terminal 4800
#No_Data

Symbol reserveW0     = w0 ; b1:b0
Symbol randomNumber  = w1 ; b3:b2
Symbol numberOfCards = b4
Symbol choosenCard   = b5
Symbol level         = b6

Symbol card1         = b11
Symbol card2         = b12
Symbol card3         = b13
Symbol card4         = b14
Symbol card5         = b15
Symbol card6         = b16
Symbol card7         = b17
Symbol card8         = b18
Symbol card9         = b19
Symbol card10        = b20

MainLoop:
  Do
    If numberOfCards = 0 Then
      Pause 2000
      SerTxd( CR, LF, "New:" )
      Gosub StartWithTenCards
    EndIf
    Gosub ChooseRandom
    Gosub ReadCard
    SerTxd( " ", #level )
    Gosub RemoveChosenCard
  Loop

StartWithTenCards:
  numberOfCards = 10
  card1  = 1
  card2  = 2
  card3  = 3
  card4  = 4
  card5  = 5
  card6  = 6
  card7  = 7
  card8  = 8
  card9  = 9
  card10 = 10
  Return

ChooseRandom:
  For b0 = 0 To 10
    Random randomNumber
  Next
  choosenCard = randomNumber ** numberOfCards + 1
  Return

ReadCard:
  Select Case choosenCard
    Case 1  : level = card1
    Case 2  : level = card2
    Case 3  : level = card3
    Case 4  : level = card4
    Case 5  : level = card5
    Case 6  : level = card6
    Case 7  : level = card7
    Case 8  : level = card8
    Case 9  : level = card9
    Case 10 : level = card10
  End Select
  Return

RemoveChosenCard:
  If choosenCard  = 1 Then : card1 = card2  : End If
  If choosenCard <= 2 Then : card2 = card3  : End If
  If choosenCard <= 3 Then : card3 = card4  : End If
  If choosenCard <= 4 Then : card4 = card5  : End If
  If choosenCard <= 5 Then : card5 = card6  : End If
  If choosenCard <= 6 Then : card6 = card7  : End If
  If choosenCard <= 7 Then : card7 = card8  : End If
  If choosenCard <= 8 Then : card8 = card9  : End If
  If choosenCard <= 9 Then : card9 = card10 : End If
  numberOfCards = numberOfCards - 1
  Return
Results ...

Code:
New: 1 6 5 10 9 2 3 4 8 7
New: 6 2 8 5 10 9 7 4 1 3
New: 9 7 6 4 3 8 2 1 10 5
New: 6 3 7 4 9 2 10 5 8 1
New: 2 9 8 5 3 4 6 1 7 10
New: 10 8 3 4 7 9 2 1 5 6
New: 10 7 9 3 6 1 4 8 5 2
New: 4 6 7 8 9 1 10 2 5 3
New: 7 5 1 6 9 2 10 4 8 3
New: 4 7 3 10 6 1 5 8 2 9
New: 7 8 2 3 5 10 4 6 9 1
New: 2 3 5 7 10 4 9 1 6 8
 

newplumber

Senior Member
Here's my implementation of the algorithm in Post #3
Thats pretty cool Hippy thanks for the implementation... perfect idea for my picaxe 20m2
I might make a big display using a few 74hc595s ... just to be simpler i would make the 10 = 0
so in all I need 10 digits...which shouldn't be to hard to drill out and throw some leds at it ... then I could get fancy and change colors for who's on what level...or not
FWIW number 10 (set of 5/set of 3) is never my friend BUT that's all about to change with my picaxe friends ideas .... well I have hope
 

techElder

Well-known member
Since you're doing all the work at your own expense, :D build a persistence-of-vision (POV) display for that Phase 10 game Level number. Raise the display up on a spinning post so all around the table can read the current random level. The trick would be to vary the scrolling so each gets their own copy to view.

Get busy! :D
 

newplumber

Senior Member
Since you're doing all the work at your own expense
not all true texas...lol I'm in the cheapest/smartest college (PFC) which for short stands for PICAXE FORUM COLLEGE! :)
I like your idea ....but seems my IQ is little short ...but I figure if I make a digital display big enough then even the blind can read the numbers :)
 

techElder

Well-known member
Phase10RandomLevel_2018-02-21-1

So, since today is full of thunderstorms and rain, I decided to explore the picaxe cloud stuff and Blockly. This random level example by Hippy seemed like a likely target. Since I don't yet have access to my PE and equipment, I haven't tested the output, but it passes syntax.

Several shortfalls in Blockly in the cloud quickly showed up.

Blockly doesn't seem to know what a byte variable is. All the variables turned out to be word variables, so after 14 were defined all the M2 picaxe chips were eliminated.

I couldn't find any way to view the output / results of this program without going back to the PE on hardware. That kind of defeats the purpose of the cloud-based application.

Well, that was entertaining, and I'm looking forward to doing some of this with my grandson, but I'll have to make sure I have hardware available for the outputs.

Phase10RandomLevel_2018-02-21-1.xml.txt <<<---- Remove the ".txt" after download for import into Blockly as xml file

View attachment Phase10RandomLevel_2018-02-21-1.xml.txt
 

hippy

Technical Support
Staff member
Blockly doesn't seem to know what a byte variable is.
That is correct. For the students Blockly is aimed at just having one size of variable has been deemed the best approach.

I couldn't find any way to view the output / results of this program without going back to the PE on hardware.
Click the "Settings" option at the top of the browser screen, and tick "Show Simulator".
 

newplumber

Senior Member
today is full of thunderstorms and rain
well texasclod ....you should move up to North Dakota its bright sunny all day every day and its 70F degrees in my picaxe institute :)

I am winning at the ten digit display ...ebay wants 3" display 6.99 a piece ... so I am building my own plus its free/instant shipping
I will place a picture later on to see if its picaxeable ...but thanks to hippy for the perfect code idea
 

newplumber

Senior Member
Okay my first attemp to a 10 digit 3" display in case some one wants to build one ...you can save 6.99 X 10 ebay = 70 + with shipping
the first wierd/horrible half display in the pic is my test ... it looks horrible in real life ...BUT I went searching at walmart for junk that might make the
light spread out more per segment (i'm using only 1 led per segment) and found if you buy their cheap semi white plastic cutting board and cut it into strips
it spreads the light kinda perfectly ... so I routered 1/8" wide 3/8" deep all around the segments and with white light it looks pretty cool unprofessionally
I am going to use two max7219 drivers way more easier to wire up/plus i have awesome code examples from all of you on the forum ... so if I have problems ...I'll let myself know :)
now back to wiring up just a few leds and of course the awesome 20m2 ....hopefully I wont get sizzling/smoke this time (but I have picaxe wire wrong insurance) :)
your friend
or not
mark
 

Attachments

Last edited:

newplumber

Senior Member
Hi everyone
I was playing around the awesome code that westaust55 made for the max7219 and I can get all 8 digits to light up perfectly
but to get data to the 2nd max7219 is not my friend here is my so far trying test code from tweeking westaust55's code

Code:
#PICAXE 20M2
#NO_DATA
 SETFREQ M32

; SYMBOL Definitions
SYMBOL DIn_7219	= PINB.0
SYMBOL Load_7219	= B.2
SYMBOL Clk_7219	= B.1

; MAX7219 Registers - 
SYMBOL No_Op	= 0
SYMBOL Digit0	= 1
SYMBOL Digit1	= 2
SYMBOL Digit2	= 3
SYMBOL Digit3	= 4 ; for more digits, dig4 thru dig7 = 5 to 8 respectively
SYMBOL Digit4	= 5
SYMBOL Digit5	= 6
SYMBOL Digit6	= 7
SYMBOL Digit7	= 8 
SYMBOL Digit8	= 9
SYMBOL Digit9	= 10
SYMBOL Decode	= 9
SYMBOL Intens	= 10
SYMBOL ScanLim	= 11
SYMBOL ShutDwn	= 12
SYMBOL DigTest	= 15

SYMBOL Set_Off	= 0
SYMBOL Set_On 	= 1
SYMBOL No_Digits	= 9 	; Scan limit for digits 0 to 3 = 4
SYMBOL Dec_Digits = 0	; decode digits 0 to 3 according to "code B"
SYMBOL Init_Inten = 12	; 0 (=1/32 PWM on time) to 15 (=31/32 PWM on time)
SYMBOL DigBlank	= 15	; "Code B" value to black a digit
SYMBOL DecimPnt	= 128 ; Add this value to a digit value/code when we want the demical point illuminated

; Define variables
SYMBOL Data_7219	= b0
SYMBOL Register	= b1

Symbol C_1   = b11
Symbol C_2   = b12
Symbol C_3   = b13
Symbol C_4   = b14
Symbol C_5   = b15
Symbol C_6   = b16
Symbol C_7   = b17
Symbol C_8   = b18
Symbol C_9   = b19
Symbol C_0   = b20

;===============================================
; Initialise the PICAXE Pins for communication with the MAX7219
;===============================================
Init:
	DIRSB = %00000111
	LOW Load_7219
	LOW Clk_7219
	DIn_7219 = 0
	
;===============================================
; Main Program loop
;===============================================	
   GOSUB Initialise7219
'$7E, $30, $6D, $79, $33, $5B, $5F, $70, $7F, $7B, $77, $1F, $4E, $3D, $4F, $47  < number code 
'  0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F

DO
   C_1 = $30 :C_2 = $6D:C_3 = $79:C_4 = $33:C_5 = $5B:C_6 = $5F:C_7 = $70:C_8 = $7F:C_9 = $7B:C_0 = $7E
    GOSUB SetDigitDisplay
        PAUSE 10000
LOOP

Initialise7219:
  Register = Decode: Data_7219 = Dec_Digits : GOSUB ShiftTo7219 
  Register = Decode: Data_7219 = Dec_Digits : GOSUB ShiftTo7219:PULSOUT Load_7219, 1
   
  Register = Intens	 : Data_7219 = 10   :GOSUB ShiftTo7219  ' DISPLAY BRIGHTNESS
  Register = Intens	 : Data_7219 = 10   :GOSUB ShiftTo7219:PULSOUT Load_7219, 1
	 
  Register = ScanLim : Data_7219 = 7  : GOSUB ShiftTo7219 ' 0 to 1    only using 2 digits on 2nd max7219
  Register = ScanLim : Data_7219 = 7  : GOSUB ShiftTo7219:PULSOUT Load_7219, 1
	    
  Register = ShutDwn : Data_7219 = Set_On     : GOSUB ShiftTo7219 
  Register = ShutDwn : Data_7219 = Set_On     : GOSUB ShiftTo7219 :PULSOUT Load_7219, 1
		  
  Register = DigTest : Data_7219 = Set_Off	  : GOSUB ShiftTo7219 
  Register = DigTest : Data_7219 = Set_Off	  : GOSUB ShiftTo7219:PULSOUT Load_7219, 1 
RETURN

SetDigitDisplay:
	Register = Digit9	 : Data_7219 = C_0 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	Register = Digit8	 : Data_7219 = C_9 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	Register = Digit0	 : Data_7219 = C_1 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	Register = Digit1	 : Data_7219 = C_2 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	Register = Digit2	 : Data_7219 = C_3 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	Register = Digit3	 : Data_7219 = C_4 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	Register = Digit4	 : Data_7219 = C_5 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	Register = Digit5	 : Data_7219 = C_6 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	Register = Digit6	 : Data_7219 = C_7 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	Register = Digit7	 : Data_7219 = C_8 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
RETURN

ShiftTo7219:
	 PULSOUT Clk_7219, 1 ; bit 15 is don't care - enable if two or more MAX7219 are cascased together
	 PULSOUT Clk_7219, 1 ; bit 14 is don't care
	 PULSOUT Clk_7219, 1 ; bit 13 is don't care
	 PULSOUT Clk_7219, 1 ; bit 12 is don't care
	DIn_7219 = bit11 : PULSOUT Clk_7219, 1
	DIn_7219 = bit10 : PULSOUT Clk_7219, 1
	DIn_7219 = bit9 : PULSOUT Clk_7219, 1
	DIn_7219 = bit8 : PULSOUT Clk_7219, 1
	DIn_7219 = bit7 : PULSOUT Clk_7219, 1
	DIn_7219 = bit6 : PULSOUT Clk_7219, 1
	DIn_7219 = bit5 : PULSOUT Clk_7219, 1
	DIn_7219 = bit4 : PULSOUT Clk_7219, 1
	DIn_7219 = bit3 : PULSOUT Clk_7219, 1
	DIn_7219 = bit2 : PULSOUT Clk_7219, 1
	DIn_7219 = bit1 : PULSOUT Clk_7219, 1
	DIn_7219 = bit0 : PULSOUT Clk_7219, 1
RETURN
In "Initialise7219:" I doubled each line after reading that register/data will go thru 1st max and into the 2nd max which seems to be working
that way so would I have to send my data (example C_1 etc) twice two
the display works awesome its just the code is messed up from ME ....I'll keep trying
your friend


UPDATE: here is a schematic of how I wired my display
I can use my 8X8 matrix code but it seems so much easier to use Decode code B for standard numbers
I must be over thinking the whole thing but my display works perfectly except the second max7219 is showing the same as the first max
back to rereading
 

Attachments

Last edited:

newplumber

Senior Member
I finally got my 10 digit display working here is my code
I am sure it can be compacted but it works awesome
it shows 1,2,3,4,5,6,7,8,9,0 on the displays then shows 0,9,8,7,6,5,4,3,2,1 in a loop
Code:
#PICAXE 20M2
#NO_DATA
 SETFREQ M32

; SYMBOL Definitions
SYMBOL DIn_7219	= PINB.0
SYMBOL Load_7219	= B.2
SYMBOL Clk_7219	= B.1

; MAX7219 Registers - 
SYMBOL No_Op	= 0
SYMBOL Digit0	= 1
SYMBOL Digit1	= 2
SYMBOL Digit2	= 3
SYMBOL Digit3	= 4 ; for more digits, dig4 thru dig7 = 5 to 8 respectively
SYMBOL Digit4	= 5
SYMBOL Digit5	= 6
SYMBOL Digit6	= 7
SYMBOL Digit7	= 8 
SYMBOL Digit8	= 9
SYMBOL Digit9	= 10
SYMBOL Decode	= 9
SYMBOL Intens	= 10
SYMBOL ScanLim	= 11
SYMBOL ShutDwn	= 12
SYMBOL DigTest	= 15

SYMBOL Set_Off	= 0
SYMBOL Set_On 	= 1
SYMBOL No_Digits	= 7 	; Scan limit for digits 0 to 3 = 4
SYMBOL Dec_Digits = 0	; decode digits 0 to 3 according to "code B"
SYMBOL Init_Inten = 12	; 0 (=1/32 PWM on time) to 15 (=31/32 PWM on time)
SYMBOL DigBlank	= 15	; "Code B" value to black a digit
SYMBOL DecimPnt	= 128 ; Add this value to a digit value/code when we want the demical point illuminated

; Define variables
SYMBOL Data_7219	= b0
SYMBOL Register	= b1

Symbol C_1   = b11
Symbol C_2   = b12
Symbol C_3   = b13
Symbol C_4   = b14
Symbol C_5   = b15
Symbol C_6   = b16
Symbol C_7   = b17
Symbol C_8   = b18
Symbol C_9   = b19
Symbol C_0   = b20

;===============================================
; Initialise the PICAXE Pins for communication with the MAX7219
;===============================================
Init:
	DIRSB = %00000111
	LOW Load_7219
	LOW Clk_7219
	DIn_7219 = 0
	
;===============================================
; Main Program loop
;===============================================	
   GOSUB Initialise7219
'$7E, $30, $6D, $79, $33, $5B, $5F, $70, $7F, $7B, $77, $1F, $4E, $3D, $4F, $47  < number code 
'  0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F

DO
   C_1 = $30 :C_2 = $6D:C_3 = $79:C_4 = $33:C_5 = $5B:C_6 = $5F:C_7 = $70:C_8 = $7F:C_9 = $7B:C_0 = $7E
    GOSUB SetDigitDisplay
        PAUSE 40000
   C_0 = $30 :C_9 = $6D:C_8 = $79:C_7 = $33:C_6 = $5B:C_5 = $5F:C_4 = $70:C_3 = $7F:C_2 = $7B:C_1 = $7E
    GOSUB SetDigitDisplay
        PAUSE 40000	    
LOOP

Initialise7219:
  Register = Decode: Data_7219 = Dec_Digits : GOSUB ShiftTo7219 
  Register = Decode: Data_7219 = Dec_Digits : GOSUB ShiftTo7219:PULSOUT Load_7219, 1
   
  Register = Intens	 : Data_7219 = 10   :GOSUB ShiftTo7219  ' DISPLAY BRIGHTNESS
  Register = Intens	 : Data_7219 = 10   :GOSUB ShiftTo7219:PULSOUT Load_7219, 1
	 
  Register = ScanLim : Data_7219 = 1  : GOSUB ShiftTo7219 ' 0 to 1    only using 2 digits on 2nd max7219
  Register = ScanLim : Data_7219 = 7  : GOSUB ShiftTo7219:PULSOUT Load_7219, 1
	    
  Register = ShutDwn : Data_7219 = Set_On     : GOSUB ShiftTo7219 
  Register = ShutDwn : Data_7219 = Set_On     : GOSUB ShiftTo7219 :PULSOUT Load_7219, 1
		  
  Register = DigTest : Data_7219 = Set_Off	  : GOSUB ShiftTo7219 
  Register = DigTest : Data_7219 = Set_Off	  : GOSUB ShiftTo7219:PULSOUT Load_7219, 1 
RETURN

SetDigitDisplay:

' second max7219  ---------------------------------------------	
	Register = Digit1	 : Data_7219 = C_0 : GOSUB ShiftTo7219  ': PULSOUT Load_7219, 1
	Register = $00	 : Data_7219 = $00 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
		
	Register = Digit0	 : Data_7219 = C_9 : GOSUB ShiftTo7219  ': PULSOUT Load_7219, 1
	Register = $00	 : Data_7219 = $00 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	
	
	
'first max7219  ------------------------------------------------	
	Register = $00	 : Data_7219 = $00 : GOSUB ShiftTo7219  ': PULSOUT Load_7219, 1
	Register = Digit0	 : Data_7219 = C_1 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
		
	Register = $00	 : Data_7219 = $00 : GOSUB ShiftTo7219  ': PULSOUT Load_7219, 1
	Register = Digit1	 : Data_7219 = C_2 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	     
	Register = $00	 : Data_7219 = $00 : GOSUB ShiftTo7219  ': PULSOUT Load_7219, 1
	Register = Digit2	 : Data_7219 = C_3 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	     
	Register = $00	 : Data_7219 = $00 : GOSUB ShiftTo7219  ': PULSOUT Load_7219, 1
	Register = Digit3	 : Data_7219 = C_4 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	     
	Register = $00	 : Data_7219 = $00 : GOSUB ShiftTo7219  ': PULSOUT Load_7219, 1
	Register = Digit4	 : Data_7219 = C_5 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	     
	Register = $00	 : Data_7219 = $00 : GOSUB ShiftTo7219  ': PULSOUT Load_7219, 1
	Register = Digit5	 : Data_7219 = C_6 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	    
	Register = $00	 : Data_7219 = $00 : GOSUB ShiftTo7219  ': PULSOUT Load_7219, 1
	Register = Digit6	 : Data_7219 = C_7 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	    
	Register = $00	 : Data_7219 = $00 : GOSUB ShiftTo7219  ': PULSOUT Load_7219, 1
	Register = Digit7	 : Data_7219 = C_8 : GOSUB ShiftTo7219  : PULSOUT Load_7219, 1
	    
RETURN

ShiftTo7219:
	 PULSOUT Clk_7219, 1 ; bit 15 is don't care - enable if two or more MAX7219 are cascased together
	 PULSOUT Clk_7219, 1 ; bit 14 is don't care
	 PULSOUT Clk_7219, 1 ; bit 13 is don't care
	 PULSOUT Clk_7219, 1 ; bit 12 is don't care
	DIn_7219 = bit11 : PULSOUT Clk_7219, 1
	DIn_7219 = bit10 : PULSOUT Clk_7219, 1
	DIn_7219 = bit9 : PULSOUT Clk_7219, 1
	DIn_7219 = bit8 : PULSOUT Clk_7219, 1
	DIn_7219 = bit7 : PULSOUT Clk_7219, 1
	DIn_7219 = bit6 : PULSOUT Clk_7219, 1
	DIn_7219 = bit5 : PULSOUT Clk_7219, 1
	DIn_7219 = bit4 : PULSOUT Clk_7219, 1
	DIn_7219 = bit3 : PULSOUT Clk_7219, 1
	DIn_7219 = bit2 : PULSOUT Clk_7219, 1
	DIn_7219 = bit1 : PULSOUT Clk_7219, 1
	DIn_7219 = bit0 : PULSOUT Clk_7219, 1
RETURN
Now all I have to do is make the final piece to the puzzle
 

newplumber

Senior Member
My phase 10 game works awesome and hippys help on the number code keeps it exciting....everyone loves it so far!



I had someone ask whats the chances of getting the game display to show the same as the card which is 1,2,3,4,5,6,7,8,9,10
so I thought oh that's EASY all you do is multiply numbers to numbers and THEN ASK PICAXE FORUM!! :)

but for reals I couldn't think of a way because I am never using the same numbers again like ...5,5,5,5,5,5

so how would I go about doing such a thing?
I came up with each digit can have 10 different numbers
and there is 10 digits
so would it be only 10X10 = 100 ? since the numbers can not be duplicates


thanks
your friend
Mark
 

hippy

Technical Support
Staff member
I am not quite sure what the actual question you are asking is.

The number generating algorithm I suggested doesn't re-use any numbers so should still fit the bill. I believe you have a 10 digit display and used 1-9 and 0 to represent 10 -

Code:
1650923487
If you are now wanting to display 0-10 you would need at least an 11 digit display though it might not be very clear which was 10 -

Code:
16510923487
A twenty digit display would allow ten two digit numbers with leading zeroes blanked, shown as "-" here -

Code:
-1 -6 -5 10 -9 -2 -3 -4 -8 -7
The issue I would have thought was in storing the number sequence digits generated somewhere, then going through those and outputting them to the display.

You could use PEEK and POKE to create a RAM array of twenty digits which can be set by the number generating routine and then output to the MAX7219 chips later.
 

hippy

Technical Support
Staff member
Here's my version of the earlier code which stores the ten cards in RAM from location 100 onwards. It uses bPtr and @bptr rather than PEEK/POKE ...

Code:
#Picaxe 20M2
#Terminal 4800
#No_Data

Symbol reserveW0     = w0 ; b1:b0
Symbol randomNumber  = w1 ; b3:b2
Symbol numberOfCards = b4
Symbol chosenCard    = b5
Symbol level         = b6

Symbol card1         = b11
Symbol card2         = b12
Symbol card3         = b13
Symbol card4         = b14
Symbol card5         = b15
Symbol card6         = b16
Symbol card7         = b17
Symbol card8         = b18
Symbol card9         = b19
Symbol card10        = b20

MainLoop:
  Do
    Gosub NewGame
    Gosub OutputResults
    Pause 2000
  Loop

NewGame:
  Gosub StartWithTenCards
  Do
    Gosub ChooseRandom
    Gosub ReadCard
    Gosub StoreResult
    Gosub RemoveChosenCard
  Loop Until numberOfCards = 0
  Return

StartWithTenCards:
  numberOfCards = 10
  card1  = 1
  card2  = 2
  card3  = 3
  card4  = 4
  card5  = 5
  card6  = 6
  card7  = 7
  card8  = 8
  card9  = 9
  card10 = 10
  Return

ChooseRandom:
  For b0 = 0 To 10
    Random randomNumber
  Next
  chosenCard = randomNumber ** numberOfCards + 1
  Return

ReadCard:
  Select Case chosenCard
    Case 1  : level = card1
    Case 2  : level = card2
    Case 3  : level = card3
    Case 4  : level = card4
    Case 5  : level = card5
    Case 6  : level = card6
    Case 7  : level = card7
    Case 8  : level = card8
    Case 9  : level = card9
    Case 10 : level = card10
  End Select
  Return

RemoveChosenCard:
  If chosenCard  = 1 Then : card1 = card2  : End If
  If chosenCard <= 2 Then : card2 = card3  : End If
  If chosenCard <= 3 Then : card3 = card4  : End If
  If chosenCard <= 4 Then : card4 = card5  : End If
  If chosenCard <= 5 Then : card5 = card6  : End If
  If chosenCard <= 6 Then : card6 = card7  : End If
  If chosenCard <= 7 Then : card7 = card8  : End If
  If chosenCard <= 8 Then : card8 = card9  : End If
  If chosenCard <= 9 Then : card9 = card10 : End If
  numberOfCards = numberOfCards - 1
  Return

StoreResult:
  If numberOfCards = 10 Then
    bPtr = 100
  End If
  @bPtrInc = level
  Return

OutputResults:
  SerTxd( "New:" )
  bPtr = 100
  For chosenCard = 1 To 10
    level = @bPtrInc
    If level < 10 Then
      b0 = "-"
      b1 = level + "0"
    Else
      b0 = level /  10 + "0"
      b1 = level // 10 + "0"
    End If
    SerTxd( " ", b0, b1 )
  Next
  SerTxd( CR, LF )
  Return
That runs in the simulator and should work in any real chip.

To output the results you should only have to change "OutputResults:" to output the data to the MAX7219 rather than show them on the Terminal display.
 

newplumber

Senior Member
hippy said:
I am not quite sure what the actual question you are asking is.
Okay sorry...I'll try to explain ...so each time we play the phase10 game with my picaxe random level decider we keep track of the game for score on paper
so when we keep track of the score we also keep track each game of how the levels are put out from the picaxe/display
example
game 11 = 9,5,7,2,8,3,1,0,4,6
game 22 = 3,1,0,4,6,5,7,2,8,9
game 34 = 2,9,5,7,3,1,4,6,0,8

and a friend asked what are the chances that the picaxe chip will show 1,2,3,4,5,6,7,8,9,0 on the display?
so I guess my question is how many different ways the picaxe using random could display each game?
and then that would answer whats the chance of displaying 1,2,3,4,5,6,7,8,9,0
and I know random isn't true random but for this question we will assume it is

I will check out your code when I get some time


and thanks again

mark
 

hippy

Technical Support
Staff member
a friend asked what are the chances that the picaxe chip will show 1,2,3,4,5,6,7,8,9,0 on the display?
Right; understand now. I misinterpreted "chances of" as meaning "possibility of having", thought you wanted to display "10" rather than just "0".

There's a one in ten chance of a 1 as the first digit, then, if so, a one in nine chance of a 2 as the second digit, a one in eight chance of 3 as the third digit, right down to a one in two chance of 9 as the penultimate digit, and a one in one chance of 0 as the last digit.

So the number of possible combinations is 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 3,628,800

And a one in 3,628,800 chance of it being that or any other particular sequence
 

AllyCat

Senior Member
Hi,

"Permutations and Combinations" is an interesting topic and often produces surprising results. Sometimes the calculation can be "elegantly simple", but it can be easy to make a wrong calculation. I believe this one is quite simple:

The first digit could be any one of ten digits but we ant a specific one, so the chances (odds) are "one in ten". The second digit is not allowed to be a "1" so the chances of a "2" are "one in nine". Similarly the chances for the "3" are "one in eight", etc. through to the final digit where the chance of a "0" (if all the other numbers are correct) is an absolute certainty.

To calculate the overall odds you must multiply all the individual odds so they become one in 10*9*8*7*6*5*4*3*2 = one in 3,628,800 , also known as ten factorial or "10!".

The "interesting" thing is that those are the odds for any specific sequence of numbers, e.g. 10,9,8,7,6,5,4,3,2,1 or your 9,5,7,2,8,3,1,0,4,6 , etc.

Ah, hippy beat me to an answer, as usual. ;)

Cheers, Alan.
 

newplumber

Senior Member
Okay that is what i wanted to know! and thanks a lot for explaining

Now I can say I have a better chance (not by much) seeing 1,2,3,4,5,6,7,8 9,0 on display then winning the lottery.
In other words... chances of my next picaxe project going right the first time is equivalent odds at replaying the same levels on phase 10


allycat said:
Sometimes the calculation can be "elegantly simple", but it can be easy to make a wrong calculation. I believe this one is quite simple:
lol i can't wait to say that....in around 2040!
 
Last edited:

hippy

Technical Support
Staff member
Now I can say I have a better chance (not by much) seeing 1,2,3,4,5,6,7,8 9,0 on display then winning the lottery.
There's a one in two chance of winning the lottery if you base it on "you will" and "you won't" :)

3,628,800 , also known as ten factorial or "10!".
It took me a while to figure out what the general mathematical formula would be for a set of consecutive digits and a fewer number of digits. For example the 4-digit padlock where numbers can be 0-9 and used only once; 10*9*8*7 = 5040 combinations.

I think it is : ( digit_max - digit_min + 1 )! / ( digit_max - digit_min + 1 - number_of_digits )!

(9-0+1)! / (9-0+1-4)!

10! / 6!

3,628,800 / 720

5040

That's correct. But that throws up an interesting case when back to the Phase 10 case, 0-9 and 10 digits ...

(9-0+1)! / (9-0+1-10)!

10! / 0!

Which would seemingly require 0! to be 1

Seems it is. And now my brain's exploded :)
 
Last edited:

AllyCat

Senior Member
Hi,

Probably the same reason that N ^ 0 (the power) is 1 ; i.e. "because it works". ;)

An interesting complication with those "mechanical push-button" combination locks is that you can press the required buttons in any sequence and the lock still opens. So rather a different calculation is required, and "more digits" is not necessarily more secure.

Cheers, Alan.
 

slimplynth

Senior Member
Interesting thought about more digits and security, I usually look for evidence of corrosion to remind me of the digits for one door I only seem to use once every couple of years..
 

hippy

Technical Support
Staff member
Good point on the 'any button order' locks with just a 0-9 keypad. That seems to reduce the possible different combinations down to a mere 252.

And, if they haven't kept the keypad clean, which keys make up the four digits can be determined, through grime or lack of grime ( or corrosion in slimplynth's case ), just one try will get you in. Even if the order is important it only takes 20 tries max.
 

hippy

Technical Support
Staff member
Probably the same reason that N ^ 0 (the power) is 1 ; i.e. "because it works". ;)
I suspect so too. The most convincing explanation I have read since is that thinking in terms of multiplying as we do to calculate factorials is not what factorials really are.

The premise there is a factorial indicates how many different ways there are to represent a set of numbers. There's a lot of ways to arrange a set of 10 numbers, but only one way to arrange a set with one number in it, and likewise only one way to arrange a set with no numbers in it.
 

slimplynth

Senior Member
I sometimes wonder (at work) if an IT based solution is the best way.. I've been told previously that I pick that type of solution not because it's the correct one but because I'm most comfortable with it.. I don't know what the answer is there but this discussion confirms RFID door passes are for the best (imo).

Afterthought: a mixture of the two ;)
 

afb

Member
For a flashcard game (teaching kids to recognise simple words) I entered the words into an array - then I swapped random elements a (largish) random number of times - then simply called the elements out in order 0 to 'N' to cycle through every word once only but in a random order. Its the equivalent of shuffling a pack of cards and then dealing them out.
 
Last edited:

newplumber

Senior Member
hippy said:
Which would seemingly require 0! to be 1

Seems it is. And now my brain's exploded :)
That's awesome you,ally and others are good at math and good at explaining how your getting the answers
I'm sure glad they didn't have math problems on the plumbing test...or I would be panhandling at walmart ;)

slimplynth said:
Interesting thought about more digits and security, I usually look for evidence of corrosion to remind me of the digits for one door I only seem to use once every couple of years..
I am just curious what kind of door would that be ...exwifes shed? fax machine room? picaxe reboot switch room? files for the population census? not even close?
 

newplumber

Senior Member
For a flashcard game (teaching kids to recognise simple words) I entered the words into an array - then I swapped random elements a (largish) random number of times - then simply called the elements out in order 0 to 'N' to cycle through every word once only but in a random order. Its the equivalent of shuffling a pack of cards and then dealing them out.
thats cool , i'm just curious how you programmed your code for the random?
 

slimplynth

Senior Member
I am just curious what kind of door would that be ...exwifes shed? fax machine room? picaxe reboot switch room? files for the population census? not even close?
:) Nothing as weird, obsolete or as cool sounding unfortunately - it's the door to a manufacturing facility that isn't open every day/week.. nor even worth visiting unless you really like spiders.. but it does have some very unique an ancient equipment/tooling...
 
Top