Random Number

DGNXFoxN
Hi there,

I've looked around but could not find a solution to my question on the forum just yet.
I would like to be able to generate a random number between 1 and 100. This would come in handy for the "ping" command that I'm trying to improve.

At this time the outcome of the command is static and looks something similar to:
"pinging servername with 32 bytes of data"
"reply from ipaddress bytes=32 time=22 TTL=55"
"reply from ipaddress bytes=32 time=21 TTL=55"
"reply from ipaddress bytes=32 time=27 TTL=55"
"reply from ipaddress bytes=32 time=23 TTL=55"

I'm trying to generate a random number in bold areas of the above example.
So that when the player does the command again he does not get the exact same response.

So it would look something more like this:
"pinging servername with 32 bytes of data"
"reply from ipaddress bytes=32 time=random number between 1-30 TTL=55"
"reply from ipaddress bytes=32 time=random number between 1-30 TTL=55"
"reply from ipaddress bytes=32 time=random number between 1-30 TTL=55"
"reply from ipaddress bytes=32 time=random number between 1-30 TTL=55"

If possible I would appreciate a solution done using the standalone GUI of QUEST.
If coding is needed, i'd appreciate a tutorial on how to and where to write the code you provided.

Thanks in advance!

HegemonKhan
I don't know of any direct GUI~Editor's choices~drop down boxes, but here's how you can do it in the GUI~Editor:

(using whatever Element: Object's Verb ~ Script Attribute, Function, Command, Turnscript, Timer, etc)

run as~a script -> add a~new script -> variables -> 'set a variable or attribute' Script -> (see below, example)

set variable game.random_ping_number = [expression] GetRandomInt (0,100)

// hopefully you understand algebra, because game making (coding~scripting~Attributes) uses a lot of variable substitutional algebra

// we're setting a global VARIABLE (an Attribute) to be equal to whatever random number (between 0 and 100) that the 'GetRandomInt' will return~output for our VARIABLE's (an Attribute) Value

// we then can use this VARIABLE (an Attribute) 'game.random_ping_number' anywhere else within our game (so long as the Object, in this case the special 'game' Game Object ~ this is the same 'game' Game Object in the GUI~Editor where you can set all of the global game's settings~options, that it is attached to, exists and still exists, of course)

you can hopefully plug this into~with the other Scripts that you need for it to work, for an example:

add a script -> ouput -> print a message -> print [expression] "Your random ping number is: " + game.random_ping_number
// outputs, an example: Your random ping number is: 73

------------------

// my way of explaining a generic Attribute syntax: Object_name.Attribute_name = Value_or_Expression
//
// Object_name: game
// Attribute_name: my own custom name for this example: random_ping_number
// Value_or_Expression: expression: GetRandomInt (0,100)
//
// this is no different than:
//
// Object Name: player
// Attribute Name: strength
// Attribute Type: int
// Attribute Value: 30
//
// or:
//
// Object Name: player
// Attribute Name: damage
// Attribute Type: int
// Attribute Value (as an Expression): player.sword.damage + player.sword.damage * player.strength / 100
//
// as can be seen:
//
// Object Name: game
// Attribute Name: random_ping_number
// Attribute Type: int // this is set from the 'GetRandomInt(min,max)' as it returns an Attribute Type: int (integer), for the Attribute's Value
// Attribute Value (as an Expression): GetRandomInt (0,100)


-----------------

references for randomization methods:

http://docs.textadventures.co.uk/quest/ ... omint.html
http://docs.textadventures.co.uk/quest/ ... ouble.html
http://docs.textadventures.co.uk/quest/ ... hance.html
http://docs.textadventures.co.uk/quest/ ... eroll.html

DGNXFoxN
@HegemonKhan
Thank you very much for this!
I have weak to no knowledge of algebra though, but I do understand what is happening and how it works now (the attributes + random numbers)
My ping command now gives a more realistic response!

Now that I understand how to work with attributes, etc... I'm also able to replace tons of code with less code and still have the same outcome.
So thank you for that!

jaynabonne
You can do something like this:

random ping.png


which corresponds to this code:

      for (i, 1, 4) {
msg("reply from ipaddress bytes=32 time=" + GetRandomInt(1, 100) + " TTL=55")
}

DGNXFoxN
@jaynabonne

I used the exact same expression, but I didn't use the loop variable... I'll see if I can edit it.

jaynabonne
The loop just gives you the "ping" repeated line output. It's up to you if you use it or not.

I just showed how to do the random number part. You might want some sort of delay to simulate the ping happening anyway between the loop iterations. (Right now, it just dumps out all four really quickly.)

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums