ramdom RND

Here we can discuss about the problem found
Post Reply
User avatar
Fernando Perez
Posts: 106
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 55 times
Been thanked: 54 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
cicciocb
Site Admin
Posts: 309
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 85 times
Been thanked: 197 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

[External Link Removed for Guests]

[External Link Removed for Guests]
User avatar
Fernando Perez
Posts: 106
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 55 times
Been thanked: 54 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:
[External Link Removed for Guests]
You do not have the required permissions to view the files attached to this post.
cicciocb
Site Admin
Posts: 309
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 85 times
Been thanked: 197 times
Contact:

Re: ramdom RND

Post by cicciocb »

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