![]() |
Forum Index : Microcontroller and PC projects : RND(number)
Author | Message | ||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1989 |
Having a problem generating a random number. The manual says If the random number is always in the range 0 to 0.999999 why specify a parameter eg. RND(100). I would expect this to return a random number between 1 and 100 but they are all decimal values below 1. How do I generate a random number between 1 and 100. Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
G8JCF![]() Guru ![]() Joined: 15/05/2014 Location: United KingdomPosts: 676 |
You multiply what RND() returns by 100 The only Konstant is Change |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1989 |
OK thanks, that works fine. Didn't understand the 0 to 0.999999 bit but I guess it is so you can generate a number below 1. Is that right. Paul "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
Justplayin![]() Guru ![]() Joined: 31/01/2014 Location: United StatesPosts: 327 |
For values from 1 to 100 you need to multiply by 100 and add 1. X = Int(RND()*100)+1 --Curtis I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
G8JCF![]() Guru ![]() Joined: 15/05/2014 Location: United KingdomPosts: 676 |
@Palcal A number between 0~0.999999 can be scaled to any other number simply by multiplying, eg Sine/Cosine etc are all between -1 and 1 and can be scaled as desired. Where this falls down is when the scaling exceeds the precision of the number, so above 10 million or so, because of single precision, multiplying doesn't work. And if you actually need a random number less than 1 then RND() as it stands is the most general/efficient implementation - anything else would require division which is a very expensive (CPU cycles-wise) operation. I hope this make sense Peter The only Konstant is Change |
||||
G8JCF![]() Guru ![]() Joined: 15/05/2014 Location: United KingdomPosts: 676 |
Hi Curtis Spot on, thanks for the correction ![]() Peter The only Konstant is Change |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1989 |
One more question. If I generate 100 random numbers between 1 and 1000 is it possible that the same number will be generated more than once. I have tried a few times and it seems that the answer is NO. Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
BobD![]() Guru ![]() Joined: 07/12/2011 Location: AustraliaPosts: 935 |
The answer has to yes unless you test for and exclude numbers already generated. The fact that it hasn't happened for you is just good or bad luck depending on your point of view. |
||||
G8JCF![]() Guru ![]() Joined: 15/05/2014 Location: United KingdomPosts: 676 |
Hi @PalCal Now we're back to the whole definition of of what constitutes "randomness" !! In the short run, ie 100 numbers, one would intuitively think that if a number repeated itself then the sequence cannot be truly random, but taken over a longer term, eg say 1 million tries, it is entirely possible and expected that a number between 1~100 would repeat itself a number of times, and consecutively, and over an even longer time frame, eg 100 million tries, the repetition and consecutive sequences would grow ever longer. By definition, a random sequence of numbers can include any sequence of numbers include an infite repetition of numbers (because infinity itself is infite) There is no such thing as true randomness, Randomness is merely what we humans perceive as the unforeseeable/unpredictable. Ever since humans have walked the earth, gamblers have tried to beat to beat the odds (and never have in the long run). So the short answer is; if you are the "house", the randomness of a number between 1~100 is not enough to ensure that you will win over the punters, hence why lottery's have sequence's of numbers, and usually at least 5 if not six numbers, even though the range of numbers seems limited, eg 1~49. I suggest that you set your program running for a week or so, and see what it turns up. This is a fascinating area where human perception meets mathematics and there is no clear winner ! Peter The only Konstant is Change |
||||
Dylan Regular Member ![]() Joined: 17/06/2013 Location: NetherlandsPosts: 81 |
Fortunately we do actually have knowledge about these things. PNRGs (pseudo-random number generators) of the simplest kind can be considered random as long as only a sequence the length of the square root is looked at. (i.e 32 bit PRNG, only useful for 2^16 random bits). Random bits repeat infinitely, on an infinite scale. They repeat roughly equally regularly too (by the law of large numbers). |
||||
HankR Senior Member ![]() Joined: 02/01/2015 Location: United StatesPosts: 209 |
|
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1989 |
Yes I knew the same number could occur more than once but did not know if something in the MMBasic command prevented it. Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1989 |
I just realised why no number appeared more than once in my tests. I generated 1000 random numbers and then sorted them into order and it was the way I sorted that prevented them appearing more than once. Back in the early 1980's I needed a sorting routine and knowing nothing about such things and with no one to ask I had to come up with my own. Now whether I reinvented the wheel or not I don't know and I still don't know anything about sorting. But this is how I did it. Lets say we have 100 numbers in an array x(1) to x(100) Using a for next loop I made another array and let n(x(1))=x(1) and so on. It was then a simple matter of using a for next loop to print out the array in order n(1) to n(100) and the numbers were sorted and that is why no number appears more than once. I did a time test for 1000 numbers and it sorts in about 1475 millisecs. a bit slow but it works. As I said I know nothing about Bubble sorts or Shell sorts or any other sorts (except good sorts) so I don't know if this is a common way of sorting or not. Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6269 |
This is the method I posted here a few months ago. ' shuffle the pack
' TassyJim Jan 2015 m = 90 ' maximum size of pack DIM pack(m) FOR n = 1 TO m ' fill the pack pack(n) = n ' in this case numbers but could be words etc NEXT n FOR n = m TO 1 STEP -1 ' each pass generates a random number from the REMAIMING number of cards k = INT(RND()*n)+1 temp1 = pack(k) ' take a random card from the pack temp2 = pack(n) ' and swap it with the top of the pack pack(n) = temp1 pack(k) = temp2 NEXT n FOR n = 1 TO m PRINT pack(n) NEXT n ' now you can select the numbers in turn for ' as many as required knowing the pack has been shuffled Jim VK7JH MMedit |
||||
Dylan Regular Member ![]() Joined: 17/06/2013 Location: NetherlandsPosts: 81 |
The way PRNGs work is generally the one documented by Knuth, namely to multiply, add constants, modulo some other constant which for sake of ease is a power of two. r1 = (a * r0 + b) mod (2^c)
If a and b are picked cleverly, then all 2^c values of r will be generated in turn. Take the simplest case, where a=0, then any b coprime to 2^c will generate all values 0 .. 2^c-1. This will have a pretty obvious pattern (for example if c=4 and b=11, then 11, 6, 1, 12, 7 doesn't look very random) but as soon as a clever choice of a is taken, all the bits in a jiggle the procession of pseudorandom numbers around. However, pseudo random numbers are rarely used at their full range of bits, and one reason for this is that the total lack of repetition is not very random behaviour. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |