Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Best practice when selecting between random choices?

Author
Message
basicFanatic
6
Years of Service
User Offline
Joined: 7th Jun 2017
Location:
Posted: 7th Jun 2018 13:35
I have always used something like the code below when I wanted my game to make a random choice about something:



printThis as string = "Press Enter"

do
// Press Enter to do something random
if GetRawKeyPressed( 13 ) = 1 then printThis = returnChoice ()

print (printThis)
Sync()
loop


function returnChoice ()

returnStatement as string
rndInt as integer
rndInt = random (1, 100)

if rndInt <= 5 REM 1-5
returnStatement = "You found nothing."
elseif rndInt <= 30 REM 6-30
returnStatement = "You found a silver coin."
elseif rndInt <= 55 REM 31-55
returnStatement = "You found a gold coin."
elseif rndInt <= 100 REM 56-100
returnStatement = "You found a basket."
endif

returnStatement = returnStatement + " " + str(rndInt)

endfunction returnStatement




But I wonder, is there a better way?
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 7th Jun 2018 14:26 Edited at: 8th Jun 2018 15:09


I wrote firm my tablet so I didn't test but you can get the idea.
PSY
Developer
7
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 9th Jun 2018 20:39 Edited at: 10th Jun 2018 02:26
Hey,

I realized I've never written a basic item drop code including loot tables, which is all about random choices, so here goes:




It basically uses this approach which is quite the standard:
http://www.lostgarden.com/2014/12/loot-drop-tables.html

I heavily commented it, so it's pretty self-explanatory.
Maybe you can use it...


Cheers,
PSY


PSY LABS Games
Coders don't die, they just gosub without return
Santman
12
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 10th Jun 2018 21:35
You can use Random2.....I believe it gives a better result.

Login to post a reply

Server time is: 2024-04-26 06:06:46
Your offset time is: 2024-04-26 06:06:46