BasicMicro - Forums

www.basicmicro.com
It is currently Mon May 21, 2012 12:36 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Random number between 1 and 8
PostPosted: Sat Jun 18, 2011 5:01 am 
Offline
Citizen

Joined: Tue Mar 29, 2011 12:01 am
Posts: 21
Hello people of the forums, i am having great difficulty trying to generate a random number between 1 and 8, all the examples on the forum i haven't been able to get my head around.. i am a nano28x owner. could somebody please provide me with an example or a direction to head in.
Thanks
this is my current code
Code:
x var long
y var long
x1 var long
y1 var long
main
x = 3
x = random x
x1 = (x/(65535/8))
x1 = x1.lowbyte
y = 3
y = random y
y1 = (y/(65535/8))
y1 = y1.lowbyte
shiftout p0, p2, fastmsbpre, [y1\8, x1\8]   'first value is row second column
pulsout p1,1
goto main

it is shifting out to cascading shiftregisters driving an led matrix


Top
 Profile  
 
 Post subject: Re: Random number between 1 and 8
PostPosted: Sat Jun 18, 2011 6:25 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 527
If it were me, I would try something real simple like:

Code:
X      var long
X1_8 var byte
; Initialize X to something random so every run is not the same.... Like timer count or the like.
loop:
    x = random x
    X1_8 = (x & 0x7) + 1 ; Get the lower 3 bits which will give us 0-7 and add 1 to get 1-8
    ...
    goto loop

If that did not appear to be random enough then I would you could grab other bits instead of the lowest 3. You could also verify the new new number was not the same as the previous one...

Kurt


Top
 Profile  
 
 Post subject: Re: Random number between 1 and 8
PostPosted: Sun Jun 19, 2011 1:55 am 
Offline
Citizen

Joined: Tue Mar 29, 2011 12:01 am
Posts: 21
worked like a charm kurt thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO