Generate a random number between 0 and X


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Jun 2007
    Location
    Mansfield, UK
    Posts
    697

    Default Generate a random number between 0 and X

    Hi, I already know i can use the RANDOM command to generate a 16 bit random number. The problem is that i dont know how to generate one that is between 0 and anything upto 255. The upper value is specified by another variable. Ive seen a few examples including one from this site that uses ** but i cant seem to get any of them to work correctly.

    Im working mainly in assembly but i dont mind examples in PBP.

  2. #2
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Hi, I already know i can use the RANDOM command to generate a 16 bit random number. The problem is that i dont know how to generate one that is between 0 and anything upto 255.
    You can use only the low byte or the high byte of the randomized word.

    Al.
    All progress began with an idea

  3. #3
    Join Date
    Jun 2007
    Location
    Mansfield, UK
    Posts
    697


    Did you find this post helpful? Yes | No

    Default

    Yes, Im using the low byte. That gives me 0-255 though. I have another variable that contains a value between 0 and 255 and i need the random number to be between 0 and that.

  4. #4
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Something like this should work:

    Rnd0 var word
    Rnd1 var byte
    Lim var byte ' your limiting variable (0 - 255)


    Random Rnd0
    Rnd1 = Rnd0 */ lim

    Al.
    All progress began with an idea

  5. #5
    Join Date
    Jun 2007
    Location
    Mansfield, UK
    Posts
    697


    Did you find this post helpful? Yes | No

    Default

    Ive not seen that operator before. What does it do? Ive had a look at the online manual and it says it "returns the middle 16 bits of the 32-bit result". Im not sure if thats what i want.

    I know i could get to the correct value using the following equation
    Rnd1 = (Rnd0.byte0 / 255) * lim

    That one wont work though because it will be rounded to 0 or 1 when dividing.

  6. #6
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    I know i could get to the correct value using the following equation
    Rnd1 = (Rnd0.byte0 / 255) * lim

    That one wont work though because it will be rounded to 0 or 1 when dividing.
    Tray to multiply first.

    Rnd1 = (Rnd0.byte0 * lim)/ 255 [which is the same as Rnd1 = (Rnd0 */ lim)]

    Al.
    All progress began with an idea

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts