ramdom RND

Here we can discuss about the problem found
Post Reply
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

ramdom RND

Post by Fernando Perez »

Cicciocb, It seems that "rdn (number)" returns only values between 0 and number-1, not between 0 and number:

Code: [Local Link Removed for Guests]

DIM frecuency(7)
timer0 100, random
wait

random:
  r = rnd(7) ' Returns a random number in the range from 0 to 7
  frecuency(r) = frecuency(r) + 1
  iteration = iteration + 1
  if iteration = 100 then
    for i = 0 to 7
      result$ = result$ + str$(frecuency(i)) + " "
    next i
    wlog result$
    iteration = 0
    result$ = ""
  end if
return
User avatar
cicciocb
Site Admin
Posts: 1899
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Re: ramdom RND

Post by cicciocb »

HI Fernando,
this is normal as the final value is not inclusive.
So RND(5) gives values from 0 to 4 (range of 5).

This is as the visual basic rnd() function that returns a value less than 1 but greater than or equal to zero.

Some references

https://www.ibm.com/docs/en/iis/11.5?to ... d-function

https://docs.microsoft.com/en-us/office ... d-function
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: ramdom RND

Post by Fernando Perez »

You're right, it's something I didn't remember.
But it is that in the help of Annex she says:
image.png
And it can confuse language novices (or not so novices).
Notice that the link you put in the first place says textually:
"expression evaluates to the total number of integers, including 0, from which the random number can be selected. That is, if n is the value of expression, the random number is generated from the numbers 0 through (n - 1)."
I think this should be clarified in the Annex help.
It has nothing to do with the subject, but for those interested in BASIC Archeology, I have found this:
https://myrapidq.it/public/BASIC_Oct64.pdf
You do not have the required permissions to view the files attached to this post.
User avatar
cicciocb
Site Admin
Posts: 1899
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Re: ramdom RND

Post by cicciocb »

Thanks Fernando.
I updated the doc (will be available in the next version)
Post Reply