Random module for dice rolls?
Moderators: David Barker, Jerry Messina
- blueroomelectronics
- Posts: 46
- Joined: Mon Apr 23, 2007 3:48 pm
- Location: Toronto
- Contact:
Random module for dice rolls?
I've been working with octal's the random number module for rolling dice and was wondering how to make it generate numbers 0-5 (not 0-255) for an upcoming JPUG http://www.blueroomelectronics.com/JPUG/JPUG.htm issue.
- blueroomelectronics
- Posts: 46
- Joined: Mon Apr 23, 2007 3:48 pm
- Location: Toronto
- Contact:
Re: Random module for dice rolls?
It's easy.blueroomelectronics wrote:... how to make it generate numbers 0-5 (not 0-255) for an upcoming JPUG http://www.blueroomelectronics.com/JPUG/JPUG.htm issue.
1- First use the LongWord version, it gives the best results (last module posted on the wiki article).
2- In the module using LongWord version, you can see in the Initialize sub
Code: Select all
Public Sub Initialize(ByVal InitialSeed As LongWord, ByVal pRndMax As LongWord = 255)
Code: Select all
public Sub SetRndMax(ByVal pRndMax as LongWord)
Code: Select all
RandGen.Initialize(2, 5) // 2 : Initial seed, and 5 the max number wanted from the generator.
or simply
RandGen.Initialize(2) // 2 : Initial seed,
RandGen.SetRndMax(5) // 5 the max number wanted
octal
- blueroomelectronics
- Posts: 46
- Joined: Mon Apr 23, 2007 3:48 pm
- Location: Toronto
- Contact:
Thanks all, I wrote a simple program to simulate 50,000 six sided dice (2d6) rolls. I noticed you need RandGen.SetRndMax(6) to get 0-5 though.
[/code]
Code: Select all
Device = 18F1320
Clock = 8
Config OSC = INTIO2, WDT = OFF, LVP = OFF
Include "usart.bas"
Include "convert.bas"
Include "RandGen.bas"
Const TotalRolls = 50000 // roll the dice
Dim Roll As Byte
Dim Count As Word
Dim Result(11) As Word
SetBaudrate(br9600)
OSCCON = $72 // 8MHz
RandGen.Initialize(2) // 2 : Initial seed,
RandGen.SetRndMax(6)
Low(PORTA.7)
High(PORTA.6)
For Count = 0 To Bound(Result)
Result(Count)=0
Next
For Count = 1 To TotalRolls
Roll = RandGen.rand()+RandGen.rand()
Result(Roll)=Result(Roll)+1
Next
Low(PORTA.6)
Write("Total of ",DecToStr(TotalRolls)," dice rolls",13,10)
For Count = 0 To Bound(Result)
Write(DecToStr(Count+2),",",DecToStr(Result(Count)),13,10)
Next
End