RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
Global Random?

0 Members and 1 Guest are viewing this topic.

***
Rep:
Level 82
We learn by living...
How do I take this script

Code: [Select]
#==============================================================================
# ** die tumbler
#------------------------------------------------------------------------------
#  This is a test script
#  it is designed to create random numbers
#==============================================================================

class Die_Tumbler
    def initialize
x = rand(10) + 1
     if x == 10
       x = 10 + rand(10)
else
   x = x
  end # end if

  y = rand(10) + 1
     if y == 10
       y = 10 + rand(10)
else
  y = y
end # end if

#print x + y
 $d20 = x + y
 
  end # end def

 
end # end class


and make it global so I can access the total of x + y?

Also, how do I access it anywhere inside the script? do I just put in something like
+ $d20 or  + Die_Tumbler

I'm really new to making classes in ruby.

P.S. I've been testing this using an event

Code: [Select]
p $d20


to see if I could access it outside itself, and was met with repeated failure and "nil".
« Last Edit: November 11, 2010, 04:44:56 PM by shintashi »

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Most Mature Member2012 Favorite Staff Member2012 Best RPG Maker User (Scripting)2012 Best MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
Well, I don't see any reason to use a class for that. Maybe a module might be better?

Like:

Code: [Select]
module Die_Tumbler
  def self.d20
    val = 0
    2.times do
      x = rand(10) + 1
      x += rand(10) if x == 10
      val += x
    end
    return val
  end
end

You could then run the method anytime with the code:

Code: [Select]
Die_Tumbler.d20
   

I am curious what you want to do with it though, since it is a very strange method. Anyway, the above code does what you apparently want to do.

Note, of course, that whenever you use it, it will rerun the method, not retain the first result. So, if you want to only do it once and operate on that result, you will need to save it to a variable.

P.S. why is your indenting so crazy? It makes it hard to read.
« Last Edit: November 11, 2010, 05:54:42 PM by modern algebra »

***
Rep:
Level 82
We learn by living...
I am curious what you want to do with it though, since it is a very strange method. Anyway, the above code does what you apparently want to do.
I've never used modules before, and only recently learned about using classes, but it worked.
The method is weird because I'm simulating a particularly quirky game mechanic known as "exploding dice". I'm using it to simulate wild surges, backlash, and botches.

Quote
Note, of course, that whenever you use it, it will rerun the method, not retain the first result. So, if you want to only do it once and operate on that result, you will need to save it to a variable.
Since it is a dice simulator, having it rerun the method every time is fine. The actual "variable" will be a byproduct of the total, ala West End's old Torg system.

Quote
P.S. why is your indenting so crazy? It makes it hard to read.
I was using spaces instead of indents to distinguish one "end" from the next, since some of the code is new to me and I tend to omit or add one too many 'ends'.