![]() |
Forum Index : Microcontroller and PC projects : Math(Mode b() ) - modal value anywhere?
Author | Message | ||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
Hi all Going to introduce students to the Math() commands today -- will be revisiting the Snakes and Ladders program. To tidy up the stats, I want to capture the count to an array and use Math(mean ()), math(median ()) etc. All good. Is there a suitable optimized function to return the modal value from an array -- e.g. math(mode b())? I appreciate that this is complex as the set of numbers could be bimodal for example. Cheers Nim Entropy is not what it used to be |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5091 |
Hi Nimue, There is no "math modal". The way I would do it is BIN it, and find the modal with searching the MAX value in the bins. - DIM an array for binning. For the snakes problem you would guess that the maximum number of rolls would be 200 or so. Or choose whatever fit's in memory (1000?). DIM BINS(200) Then execute the program where every answer adds up to the related cell in the BINS() array. BINS(rolls)=BINS(rolls)+1 Then after finalizing the 10000 runs MODAL = MATH (MAX BINS()) Regards, Volhout PicomiteVGA PETSCII ROBOTS |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
Nice - worked a treat. Thankyou -- some nice explaining for students on how this works. The index of an array found from the value in another array. Nice. In the board designed below - interesting to see the range of rolls. ![]() edit: edited to return the index of the bin (ie roll value - and not just the count) ' Snakes and Ladders simulation ' Start off the board (position = 0) ' Only end when landing exactly on 100. If over roll, move back ' the surplus amount. So on 99, roll 4 (=103). Move forward 1 + ' (=100) then back 3. Final square = 97. option base 1 ' Board starts at square 1 option default integer 'variables integers by default dim board(100) dim bins(500) dim binindex(500) ' Pre populate board with squares that equal its number for x = 1 to 100 let board(x) = x next x ' load in structure of snakes and ladders for y = 1 to 18 read start,finish let board(start) = finish next y 'reset rolls and set number of simulations let roll_total = 0 let maxit = 10000 '10,000 gives a consistent overall average dim rolls(maxit) print "Simulating"maxit " individual games of Snakes and ladders." 'set up the simulation for runs = 1 to maxit let position = 0 ' start off the board let currnetroll = 0 do 'individual games currentroll = int((rnd*6))+1 position = position + currentroll if position = 100 then exit do if position > 100 then position = 100 - (position - 100) position = board(position) rolls(runs) = rolls(runs) + 1 'keep count of number of rolls loop while position <100 bins(rolls(runs)) = bins(rolls(runs)) + 1 next runs roll_mean = math(mean rolls()) roll_median = math(median rolls()) roll_max = math(max rolls()) roll_min = math(min rolls()) sort bins(), binindex() roll_modal = binindex(500) print"" print "Mode:"roll_modal, "Mean:" roll_mean, "Median:" roll_median, "Max:" roll_max, "Min:" roll_min 'ladders and then snakes 'data 5,23,7,55,13,31,20,39,33,69,37,86,57,83,61,99,71,92 'data 45,9,47,11,63,43,74,49,77,51,81,26,88,3,94,53,96,41 'board 2 data 5,17,13,31,21,37,28,64,48,52,54,91,62,98,76,86 data 26,7,33,10,57,53,59,24,67,36,71,49,77,41,84,61,92,73,97,66 Edited 2022-11-01 21:18 by Nimue Entropy is not what it used to be |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
Final update - day job beckons. ![]() Entropy is not what it used to be |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5091 |
Hi Nimue, I guess you are running this on a CMM2, since my picomite cannot run your simulation. I guess youd DIM rolls(maxit) simply asks too much memory for the small picomite. By creating 2 arrays ( bins() and binindex() ) you may be overcomplicating the algorythm. I use this (based on the first post snakes and ladders program). option base 1 dim board(100) 'the 100 squares of the board dim bins(500) 'gather statistics of the runs (is zero'ed at creation) ' Snakes and Ladders simulation ' Pre populate board with squares that equal its number for x = 1 to 100 let board(x) = x next x ' load in structure of snakes and ladders from DATA statements for y = 1 to 18 read start, finish let board(start) = finish next y 'reset rolls and set number of simulations let roll_total = 0 let maxit = 10000 '10,000 gives a consistent overall average print "Simulating"maxit " individual games of Snakes and ladders." 'set up the simulation for runs = 1 to maxit let position = 1 let rolls = 0 do 'individual games roll = int((rnd*5)+0.5)+1 position = position + roll if position >= 100 then exit do position = board(position) rolls = rolls + 1 loop while position <100 roll_total = roll_total+rolls bins(rolls)=bins(rolls)+1 next runs print "Overall average after" maxit " rolls is" int((roll_total/maxit)+0.5) " rolls" ' calculate modal% value (the index in the array) and qty (the number of times it is ' rolled. qty=math(max bins(),modal%) print "modal value = ";modal%;" occurring ";qty;" times" 'ladders and then snakes data 5,23,7,55,13,31,20,39,33,69,37,86,57,83,61,99,71,92 data 45,9,47,11,63,43,74,49,77,51,81,26,88,3,94,53,96,41 Please note that the MATH(MAX ...) command allows you to read the max value, but also the index at which this max value occurs. So in one command you have the index, and the value.... Edited 2022-11-01 23:01 by Volhout PicomiteVGA PETSCII ROBOTS |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
Nice - updated, removed the unnecessary second array and the sort function. Currently using MMBasic Windows as this was "in class" -- now more a rabbit hole I seem to have gone down and the students are less interested than me. Interesting how many variations on board design there are (Snakes and Ladders) and how homogenous "Chutes and Ladders" boards are in comparison. N Entropy is not what it used to be |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5091 |
There is 1 improvement needed: When you roll a dice, the value is taken from the RND statement. The new position is calculated, and when 100, you exit. But only afterward the number of rolls is incremented. So your answer is wrong by 1. Move the "rolls = rolls + 1" before the "exit do" PicomiteVGA PETSCII ROBOTS |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
Not all boards are the same... ![]() ![]() I imagine that 800+ rolls games would end with me tipping the board over ![]() ![]() N Entropy is not what it used to be |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7938 |
It's a very old game and probably has multiple variations for every country now. Not all of them use 10x10 boards and not all of them are unbiased - some having more "snakes" than ladders (or vice versa). "Chutes and Ladders" variations seem to be US only, produced because (adults thought that) children were a bit uncomfortable with snakes. Many have a very moral theme to them. There are lots of variations with the boards, most showing some sorts of reward and punishment. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
![]() ![]() Thank you. N Entropy is not what it used to be |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |