Need help to understand some code, random number within a range of.

Anything QL Software or Programming Related.
User avatar
vanpeebles
Commissario Pebbli
Posts: 2077
Joined: Sat Nov 20, 2010 7:13 pm
Location: North East UK

Need help to understand some code, random number within a range of.

Postby vanpeebles » Wed Jul 10, 2019 9:53 am

Now first up, this is more of a maths question, rather than a how do I code this. This is in ACT adventure code (no groaning at the back!!) In various places the original programmer, can get a range of set random numbers using divide, but I don't know how the division number applies to the amount of choices that you want/need. If I can get my head round this, I can add little random description messages per location, or even make future adventures semi random each time you play (eg changing room colours, size descriptions etc)

Manual States:

In this case, the use of the routine 'random_message3' requires variable 1 to contain the number of the middle message of a group of three.

9880 NAME random_message3:RANDOM 0:DIVIDE 0,16385:VAR_ADD 1,0:PRINT_GEN 0:STOP

Line 74: It's already open.
Line 75: It is open already.
Line 76: No, it's already open.

RANDOM Vn : A random number (range -32768 to +32767) is loaded into Vn.
DIVIDE Vn,#m : The value in Vn is divided by #m.
VAR_ADD Va,Vb : The contents of Va are added to those of Vb.

So 75 is already known to us, and passed in via variable 1 on var_add

Another example chooses from five messages (middle message is number 15)
2350 RANDOM 0:DIVIDE 0,10923:ADD 0,15:PRINT_GEN 0:RETurn
using 10932 returns a choice of five messages 13, 14, 15, 16, 17

So if I want say, a range of ten, what would my 16385 or 10932 value be? I don't understand how this figure is worked out? I know what my message numbers are, say 10 in a row starting at 45 to 55, I'd pass 50 either directly or with a var, but it's the division figure I'm stuck on. :?:

I should have paid more attention to Johnny Ball :cry:


User avatar
tofro
QL Wafer Drive
Posts: 1596
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Need help to understand some code, random number within a range of.

Postby tofro » Wed Jul 10, 2019 10:26 am

Peter,

Let's say you want a number between 50 and 100:

RANDOM gives you a number -32768-32767

scale that into -25..25 (you want a range of 50)

DIVIDE 0,1311

(1311 is 32767/25, i.e the max/min value you want)

gives you a value between -25 and +25

ADD 75

gives you a value between 50 and 100.

Adaption to your exact problem is left as an exercise to the reader (and a cup of coffee ;) )

Hint: work with the min and max values and see if you land in the area you're targetting.

Tobias
Last edited by tofro on Wed Jul 10, 2019 11:26 am, edited 1 time in total.


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
vanpeebles
Commissario Pebbli
Posts: 2077
Joined: Sat Nov 20, 2010 7:13 pm
Location: North East UK

Re: Need help to understand some code, random number within a range of.

Postby vanpeebles » Wed Jul 10, 2019 10:48 am

Many thanks! I think I kind of get it, In the second example above, how does 10932, equate to a range of 5 numbers?


User avatar
vanpeebles
Commissario Pebbli
Posts: 2077
Joined: Sat Nov 20, 2010 7:13 pm
Location: North East UK

Re: Need help to understand some code, random number within a range of.

Postby vanpeebles » Wed Jul 10, 2019 10:49 am

vanpeebles wrote:Many thanks! I think I kind of get it, In the second example above, how does 10932, equate to a range of 5 numbers?


Roughly divided by 3 and rounded up/down?


User avatar
pjw
Super Gold Card
Posts: 530
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: Need help to understand some code, random number within a range of.

Postby pjw » Wed Jul 10, 2019 11:21 am

tofro wrote:<>
Let's say you want a number between 50 and 100:

RANDOM gives you a number -32768-32767
Am I missing something here? What about RND(50 TO 100)? or RND(-50 TO 50)? RND already does all the hard work for you..


Per
Be wary of large West-Coast corporations bearing gifts!
- Ancient Trojan proverb
User avatar
tofro
QL Wafer Drive
Posts: 1596
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Need help to understand some code, random number within a range of.

Postby tofro » Wed Jul 10, 2019 11:27 am

pjw wrote:Am I missing something here? What about RND(50 TO 100)? or RND(-50 TO 50)? RND already does all the hard work for you..


Unfortunately for Pete he's not working with SuperBASIC, but with ACT (Adventure creation tool)

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
tofro
QL Wafer Drive
Posts: 1596
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Need help to understand some code, random number within a range of.

Postby tofro » Wed Jul 10, 2019 11:41 am

vanpeebles wrote:Many thanks! I think I kind of get it, In the second example above, how does 10932, equate to a range of 5 numbers?


10932: Not at all.

You want a number between 13 and 17. That is a range of 5. Thus, divide the complete range of random numbers RANDOM gives you by 5 to get the proper divisor:

65535/5= 13107
now

DIVIDE 0,13107

will now give you a value between -2.5 and +2.5 (i.e. range of 5)

now add 15 to get you in the range of 12.5 to 17.5

ADD 0,15

and you should be there. Due to integer rounding, you may need to round up or down your values in places (that is, give or take 1) - I'm too busy sort that atm.

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
tcat
Super Gold Card
Posts: 553
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Re: Need help to understand some code, random number within a range of.

Postby tcat » Wed Jul 10, 2019 11:45 am

Hi,

Nice.
`ACT' defines its own language, I can only admire.

Slightly off topic. Is it possible to generate randoms just using integer maths only?

Thinking further..
I wonder what it takes, to make a simple adventure game engine, nothing to compare with ACT, just a proof of concept sort of thing.

Being zero on the subject, it reminds me of a `state machine', changing states on iterraction with objects, characters, and environments. Then it must also implement a language and vocabulary, with some interpreter in place. Is this like a programming language with its syntax and semantics?

Tomas


User avatar
vanpeebles
Commissario Pebbli
Posts: 2077
Joined: Sat Nov 20, 2010 7:13 pm
Location: North East UK

Re: Need help to understand some code, random number within a range of.

Postby vanpeebles » Wed Jul 10, 2019 11:56 am

tofro wrote:
vanpeebles wrote:Many thanks! I think I kind of get it, In the second example above, how does 10932, equate to a range of 5 numbers?


10932: Not at all.

You want a number between 13 and 17. That is a range of 5. Thus, divide the complete range of random numbers RANDOM gives you by 5 to get the proper divisor:

65535/5= 13107
now

DIVIDE 0,13107

will now give you a value between -2.5 and +2.5 (i.e. range of 5)

now add 15 to get you in the range of 12.5 to 17.5

ADD 0,15

and you should be there. Due to integer rounding, you may need to round up or down your values in places (that is, give or take 1) - I'm too busy sort that atm.

Tobias


Ah, it's 65535/x. So, I'm hoping a range of ten would be 6553. You don't have to worrying about the roundings, as ACT sorts that :)


User avatar
vanpeebles
Commissario Pebbli
Posts: 2077
Joined: Sat Nov 20, 2010 7:13 pm
Location: North East UK

Re: Need help to understand some code, random number within a range of.

Postby vanpeebles » Wed Jul 10, 2019 12:20 pm

My idea was having say a list of descriptors, such as gold, copper, silver, iron etc. Then at the start of the game, it would run through and change them all at random on the objects. For ACT, I'd have to make sure my word list, is numbered the same as my description message range, then it would be a case of setting message range number to the word number on the object, after randomisation. So it would be the same.

For locations, I don't think there is a way to do this in ACT, I'd have to think on that.

For my current game, I was hoping to implement a random text on screen for idling in an area. So each room would have maybe 3 or 4 messages, picked at random after a while.

*A florescent ceiling light flickers*
*A solitary vehicle whirs past the building and into the distance*
*The swipe panel LED glows softly in the gloom*

Just to add a bit of atmosphere.



Who is online

Users browsing this forum: No registered users and 5 guests