Function Rand(Range As Byte) As Byte
Dim i As Byte, feed As Bit, temp As Word
For i = 0 To 7 //generate 8 bits
Feed = Seed.30 Xor Seed.27 //make new bit
Seed=Seed*2+Feed //shift seed left and add new bit
Next
Temp=(Seed And 255) * Range //change Rand from 0 to 255
Rand = Temp/256 //to 0 to (Range-1)
End Function
RandomNumber = (TimerValue MOD (MaxDesiredValue)) + 1
'timer setup for PIC18F25K22
'
T1CON = %01000011
T1GCON = %00000000
If S1 = Pressed Then
TimerValue.byte0 = TMR1L
TimerValue.byte1 = TMR1H
Rand = (TimerValue Mod 5) + 1
USART.Write("Timer Value : ", DecToStr(TimerValue)," Rand: ", DecToStr(Rand), 13, 10)
DelayMS(367)
End If
As tumbleweed stated a MrDEB topic or two ago, the answers to most MrDEB questions can be found in previous MrDEB posts.Back in 2009 MrDeb wanted to build a critter ridder, I posted full working code which included a random number generator. It was a psuedo Random number but worked for this instance. I repeat it here for any comments,
{
return (rand() % (upper - lower + 1)) + lower;
}
It changes values to be within the supplied range - rand() itself returns a value between 0 and RAND_MAX (a constant set in the compiler, which is at least 32767).The use of modulo to limit the span has bothered me since I used it in an earlier post. In brief, we know you can do certain manipulations to a random set without biasing the distribution, except as is obvious. For example, you can multiply every number by 2. Of course, the result doesn't include odd values, but the remaining distribution is not changed. You can add to every value or delete all of certain values without changing the relative distribution of the remaining values, but modulo is different. It adds to some but not to all values.. At least as I understand how it is being used.
Take the values 0...9 and do mod5. The distribution of values 0 to 4 are not changed initially, but 5 becomes 0, which adds to the frequency of 0 and so forth for 6,, 7, 8, and 9. Thus, 5 values are affected and add to 5 existing values. Maybe that's OK as done in this thread. But what if you wanted to limit the range to 0...7 and did a %8 conversion? An 8 would increase the frequency of 0's and 9 would increase 1's.. The frequencies of 2...7 are not affected. That disturbs the randomness.
I mean this as a question, as I do not understand the C function. Does this:
Code:{ return (rand() % (upper - lower + 1)) + lower; }
delete values outside the range or change their values to be in range?
I don't think it does, as it effectively picks out different 'ranges' - and as it's supposed to be 'random' there doesn't want to be any 'distribution'. Deleting values out of range would be a complete disaster, and serious skew the results - not to mention taking a VERY long time.Thanks, Nigel,
That's what I thought it did and suspect it disturbs the distribution, except as in the case of 5, where the changes are "balanced." Perhaps, it is better in the general case to delete values out of range.
I always use this method if I need to shuffle any number of items. It works perfectly and has a fixed execution time.The easy way is to create a 52 byte array and store the numbers 1 to 52 sequentially in them. You then 'shuffle' the pack by looping through 52 times selecting each address in turn (so for 'x = 1 to 52') and swapping each value with a randomly selected address between 1 and 52. So you always do a simple 52 times loop, and create a nicely shuffled pack - then you simply take cards off the 'top' of the pack in sequential order. It can obviously be applied to any similar situation.
Last time I used it was in Delphi, to write a simple Windows program to generate random lottery numbers, for a lottery syndicate at work.I always use this method if I need to shuffle any number of items. It works perfectly and has a fixed execution time.
Mike.
I just did a Google search and found this: https://crypto.stackexchange.com/qu...~:text=1) Does modulo affect the,Yes, it does.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?