Random module for dice rolls?

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
blueroomelectronics
Posts: 46
Joined: Mon Apr 23, 2007 3:48 pm
Location: Toronto
Contact:

Random module for dice rolls?

Post by blueroomelectronics » Thu Jun 12, 2008 12:24 am

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.

normnet
Posts: 55
Joined: Mon Jan 01, 2007 6:32 pm

Post by normnet » Thu Jun 12, 2008 7:06 am

Divide by 51

Norm

User avatar
blueroomelectronics
Posts: 46
Joined: Mon Apr 23, 2007 3:48 pm
Location: Toronto
Contact:

Post by blueroomelectronics » Thu Jun 12, 2008 3:42 pm

roll / 51 will only roll a 6 (5+1) once every 255 rolls, those would be very loaded dice.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Re: Random module for dice rolls?

Post by octal » Thu Jun 12, 2008 9:06 pm

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.
It's easy.
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)
You can pass a pRndMax (default to 255). this is the max number wanted. You can also set it using the sub

Code: Select all

public Sub SetRndMax(ByVal pRndMax as LongWord)
to get numbers between 0-5 just do

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 
Regards
octal

normnet
Posts: 55
Joined: Mon Jan 01, 2007 6:32 pm

Post by normnet » Thu Jun 12, 2008 11:38 pm

roll / 51 will only roll a 6 (5+1) once every 255 rolls, those would be very loaded dice.
mistake
To scale 0-255 to 0-5 should be:

if roll = 255 then
roll = 254

roll = roll * 10 / 425


Norm

User avatar
blueroomelectronics
Posts: 46
Joined: Mon Apr 23, 2007 3:48 pm
Location: Toronto
Contact:

Post by blueroomelectronics » Sat Jun 21, 2008 4:44 am

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: 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
[/code]

Post Reply