Random


Closed Thread
Results 1 to 3 of 3

Thread: Random

  1. #1
    SELTZER's Avatar
    SELTZER Guest

    Lightbulb Random

    I Want To Use This In A Program, In The Pic Basic Book It Says That Var Is A Seed And The Holder Of The Final Data. How Do I Use Var In This Case. I Need A Sample.

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    I though the manual pretty much said it perfectly...

    MyWord var WORD ' Variable must be a WORD and must NOT be an array.

    .. .. ..

    MyWord=1234 ' Word must contain a starting number or 'seed'

    .. .. ..

    Random MyWord ' Random generates a new number conained in MyWord

    Random takes the 'seed' (your starting value) you gave it somewhere at the start of your program, and uses it to generate a new value which then replaces the previous contents of the variable MyWord. Thereafter you can simply use that result to keep on generating new numbers, each will replace the previous one.

    Dice Example
    Code:
    	RandomValue var WORD
    	Dice var BYTE
    
    	ButtonPress var PortB.0
    
    	RandomValue var 9876
    
    Loop:
    	If ButtonPress=1 then goto Loop	' Button Not Pressed
    	Random RandomValue		' Generate Random Value
    	Dice=(RandomValue//6)+1		' Convert it to range 1-6
    	Pause 75			' Debounce Delay for Button
    	While ButtonPress=0:Wend	' Wait for Button to be Released
    	Gosub DisplayDice		' Display Result
    	Goto Loop			' Do it again
    The starting 'seed' value always ensures that the Random command starts it's sequence from the same repeatable point (so you can repeat EXACTLY the same sequence each time you run your code). This is called pseudo-random. If you want TRUE random, then you must start with a different seed each time.

    This second example shows a way of ensuring your sequence is never repeated (unless you reprogram the PIC)...
    Code:
    	RandomValue var WORD
    	Dice var BYTE
    
    	ButtonPress var PortB.0
    
    	Data @0,148,38			' Just a Starting Seed value
    	Read 0,Randomvalue.Lowbyte	' Load RandomValue from EEPROM
    	Read 1,Randomvalue.Highbyte
    
    Loop:
    	If ButtonPress=1 then goto Loop	' Button Not Pressed
    	Random RandomValue		' Generate Random Value
    	Dice=RandomValue//6)+1		' Convert it to range 0-5
    	Dice=Dice+1
    	Pause 75			' Debounce Delay for Button
    	While ButtonPress=0:Wend	' Wait for Button to be Released
    	Gosub DisplayDice		' Display Result
    	Write 0,RandomValue.Lowbyte	' Save Random Value for Future use
    	Write 1,Randonvalue.Highbyte
    	Goto Loop			' Do it again

  3. #3
    SELTZER's Avatar
    SELTZER Guest


    Did you find this post helpful? Yes | No

    Lightbulb Random

    Thanks for your help

Similar Threads

  1. Random Numbers
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 18th December 2008, 08:47
  2. 6 random numbers limiting to 49 ?????
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 15th June 2005, 23:27
  3. Random number results
    By bartman in forum mel PIC BASIC
    Replies: 3
    Last Post: - 9th March 2005, 17:39
  4. Random function - How to limit it ??
    By martarse in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 30th November 2004, 14:05
  5. more random number questions
    By bartman in forum mel PIC BASIC
    Replies: 1
    Last Post: - 14th November 2004, 17:55

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts