Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 07:38 14 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : RND(number)

Author Message
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1989
Posted: 02:55pm 16 Apr 2015
Copy link to clipboard 
Print this post

Having a problem generating a random number. The manual says
  Quote  Returns a pseudo-random number in the range of 0 to 0.999999.

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 Kingdom
Posts: 676
Posted: 02:58pm 16 Apr 2015
Copy link to clipboard 
Print this post

You multiply what RND() returns by 100
The only Konstant is Change
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1989
Posted: 03:10pm 16 Apr 2015
Copy link to clipboard 
Print this post

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 States
Posts: 327
Posted: 03:16pm 16 Apr 2015
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 676
Posted: 03:20pm 16 Apr 2015
Copy link to clipboard 
Print this post

@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 Kingdom
Posts: 676
Posted: 03:22pm 16 Apr 2015
Copy link to clipboard 
Print this post

Hi Curtis

Spot on, thanks for the correction

Peter

The only Konstant is Change
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1989
Posted: 03:53pm 16 Apr 2015
Copy link to clipboard 
Print this post

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: Australia
Posts: 935
Posted: 04:35pm 16 Apr 2015
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 676
Posted: 04:36pm 16 Apr 2015
Copy link to clipboard 
Print this post

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: Netherlands
Posts: 81
Posted: 10:58am 17 Apr 2015
Copy link to clipboard 
Print this post

  G8JCF said  
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.


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 States
Posts: 209
Posted: 11:14am 17 Apr 2015
Copy link to clipboard 
Print this post

  palcal said   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.[/QUOTE

Not only is it possible, it should happen just about every time.

This is a general example of the birthday problem in probability.

Birthday problem

The birthday problem solution shows that it only takes a small group
of people to get one or more people with the same birthday. Somewhat
amazingly, because it runs counter to our intuition, a group of only
23 people will yield on average an occurrence of at least one pair
of matching birthdays with a probability of 50.7 % -- about half
the time.

A group of only 40 people brings it way up to matching 80.1% of the
time.

This is exactly analogous to drawing a set of random numbers of range
1 to 365 and asking how often will there be a number repeated.

Using the exponential approximation formula given by Wikipedia, the
chance of at least one number in a random range 0-1000 being repeated
for a string of 100 picks is approximately 99% [ 1-(.999^4950) ]!

To make this easier to accept, just think of the 100th and final
selection. There are already 99 numbers that have been picked, so
the chance of a number from 0-1000 matching one of those already
picked is 99/1001 ~ 10%. That's just considering the last pick,
and there are 98 other chances before that to make a match.

Just to verify all the above, I ran an Open Office spreadsheet using the
built-in random function. There were eight groups of 50 numbers picked
over a range of 0-1000. I looked for matches by eye, so that's why I
didn't go all the way up to picking 100 numbers (you can go bonkers).

According to the exponential approximation, there should be a repeat
71% of the time. I got repeats in 5 out of 8 trials for 62%, which seems
like good verification of the calculation for such a small number of
trials.

So if you don't almost always get a repeat in 100 numbers, there's
something wrong somewhere.

Hank
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1989
Posted: 11:25am 17 Apr 2015
Copy link to clipboard 
Print this post

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: Australia
Posts: 1989
Posted: 12:41pm 17 Apr 2015
Copy link to clipboard 
Print this post

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: Australia
Posts: 6269
Posted: 09:11pm 17 Apr 2015
Copy link to clipboard 
Print this post

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: Netherlands
Posts: 81
Posted: 09:55pm 17 Apr 2015
Copy link to clipboard 
Print this post

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.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025