Jump to content

Generating a random integer within a set range?


Karatekid5

Recommended Posts

I know this may be super simple to a lot of you but a rather simple script is giving me a lot of headaches. The script is intended for my Shiny Creatures mod and I want each creature that spawns to run its' own instance of the script for a chance to spawn the shiny in its' place. To test this iteration of the script, I turned up the value range to a range of 1-4, with a value of 4 being checked for and intended to spawn the shiny if landed on (and it is intended that each creature running the script would run this randomizer for its' own . I also attached a message to verify that it's working. Because I had to use the GameMode Begin block the script seems to run constantly (message never disappears) even if I'm in a cell with no actors that run it. Furthermore, I test ran the script many times and no shinies spawned from it, so the randomizer seems to be never hitting the correct value, providing that it's even running at all.

Here is the current script:

scn ShinyCreatureMudcrab

begin GameMode

int shinyvalue

message "Mudcrabs on the run!"

int shinyvalue = Utility.RandomInt(1,4)

  if (shinyvalue == 4)
     Disable();
    PlaceAtMe "ShinyMudCrab" 1 0 0;
   endIf

end

This code compiles with no errors. I also tried RandomPercent to no avail. Thanks in advance for any help!

Link to comment
Share on other sites

I did check both of those out and for a mod with very simple scripts I don't want to make the Oblivion Script Extender a requirement.

 

I gave GetRandomPercent a run and all instances of the script had the problem where they all return the same value. Is there a way around this?

Link to comment
Share on other sites

Something like this should work:

 

short rnd
set rnd to Getrandompercent    

if shinyvalue >= 75
    Disable();
    PlaceAtMe "ShinyMudCrab" 1 0 0;
endIf

Based on the note in the Wiki page, It may be possible that this function returns the same number number on all calls in the same frame (not sure and I don't have Oblivion installed to check it), but it, certainly, should return a random number over multiple frames.

Link to comment
Share on other sites

I need to think outside the box a little more xD I got too hung up on using integers and didn't think of comparing to a percentage instead. The code works perfectly, and thank you very much! I got intense lag at first but then I realized I forgot to close the loop, which spawned a lot of shinies in one spot! Thankfully that was an easy fix!

Just a couple more questions to wrap things up. Can I put a decimal like 99.997 (for 1/300 odds) as a value to compare to or does it have to be a full integer? and does Oblivion have any kind of delete command? I did a lot of research and couldn't find anything in Oblivion's script command libraries. Morrowind has SetDelete and Skyrim has Delete() so I'd assume Oblivion has one as well.

 

Thank you again for everything!

Link to comment
Share on other sites

Getrandompercent returns only integers, therefore the smallest percentage you can consider is 1% (like if shinyvalue = 99)

 

But you could use a second roll

if shinyvalue = 99
    if shinyvalue >66
        <this code would run 1/300 times>

(Although this would not work if the function returns the same value in the same frame)

 

 

Rand returns floating point number, but, as you noted, requires OBSE.

Link to comment
Share on other sites

I decided to test run the second roll by spawning a lot of test mudcrabs with the console and it had no issues spawning any shinies, but I'd have to try it more extensively to fully verify the odds. Though I assume all of these scripts run when the player enters the cell containing the creature, so in a functional scenario I wonder if all creatures in a cell load on the same frame.

Link to comment
Share on other sites

Yes, all scripts run when the player enters the cell containing the creature (assuming it is an internal cell)

Yes, all creatures in a cell load on the same frame.

 

The bottom line is that you project depends on whether the function always returns the same value in the same frame.

 

I suggest you check it with something like this

short rval1
short rval2

set rval1 to Getrandompercent  
set rval2 to Getrandompercent  
Message "rval1: %.0f     rval2: %.0f " rval1 rval2
Edited by QQuix
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...