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.
[RESOLVED] random number generator problem

0 Members and 1 Guest are viewing this topic.

***
Rep:
Level 87
A variable in my game (acting as an actor attribute) is a random number. I use var = rand(#) to generate it...blah blah
The problem is: the value is supposed to be determined randomly and then remain constant FOR THE REST OF THE GAME, but using the rand( function makes it ever changing!

How do I tell the script to determine the number as random, but then save that first result as the value permanantly?
« Last Edit: December 20, 2007, 11:33:41 AM by Zeriab »

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
You run the code just once...

Is it that you want lazy initialization?

***
Rep:
Level 87
run it once? how?

my script looks like:
Code: [Select]
x = 25
n = rand(50)
return x * 2 + n
this is for something like max HP, but the max and total HP are different and it changes everytime i close and re-open the menu. How do i keep it from doing this? (btw im brand new at scripting and learning quickly if u havent noticed)

*
Rep:
Level 102
2014 Biggest Narcissist Award2014 Biggest Forum Potato2014 Best Non-RM Creator2013 Best Game Creator (Non-RM)2013 Best IRC ChatterboxParticipant - GIAW 112012 Most Successful Troll2012 Funniest Member2012 Best Use Of Avatar and Signature space2012 Best IRC ChatterboxSecret Santa 2012 ParticipantProject of the Month winner for November 2009For being a noted contributor to the RMRK Wiki2010 Most Successful Troll2010 Biggest Forum Couch Potato2010 Best IRC Chatterbox
You know there's a random number generator event command.

***
Rep:
Level 87
yes i no that but it might not work correctly if i change the value that way. Cuz it's an actor attribute in the Game_Actor script which performs on multiple objects, so it might make every characters....rand_str (just an example) stat ALL THE SAME random #. Where as I want every characters different

*
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 Veteran2011 Favourite Staff Member2011 Most Mature Member2011 Best RPG Maker User (Scripting)2011 Best Use of Avatar and Signature Space2010 Most Mature Member2010 Favourite Staff Member
Well, you need to initialize it just once. If it's something in Game_Actor, then the best thing to do would be to put it in the initialize method of Game_Actor. If you put it into the initialize method, then it will not be the same random number for every actor either. If you need to save that random number, then save it as a class variable and it will remain constant for the entire game. Or you could even make it a constant.

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
Code: [Select]
class Game_Actor
  attr_reader :random_hp_add
  def random_hp_add=(value)
    return  unless @random_hp_add.nil?
    @random_hp_add = value
  end
end

Usage:
Code: [Select]
x = 25
actor.random_hp_add = rand(50)
return x * 2 + actor.random_hp_add

actor is here assumed to be a Game_Actor. If you are in the Game_Actor class itself then just use 'self.random_hp_add' instead of 'actor.random_hp_add'
« Last Edit: September 11, 2007, 09:53:46 PM by Zeriab »

***
Rep:
Level 87
thx zeriab ur class variable method ALMOST worked...it does stay constant, but while i can tell both characters had at sometime had different values, (there total HP starts different) their maxhp....'corrects' itself and they all become equal. -_-

thx again for helping though

*
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 Veteran2011 Favourite Staff Member2011 Most Mature Member2011 Best RPG Maker User (Scripting)2011 Best Use of Avatar and Signature Space2010 Most Mature Member2010 Favourite Staff Member
ermm... Are you retaining the original levelling method? Because, if you want your value to effect all levels you will need to overwrite that levelling method.

***
Rep:
Level 87
no, im using my own level method actually.

What i mean is that all ACTORS have the same random_value which I don't want. I used a max/min like HP b/c it actually doubles the test results. (total HP and max HP being two separate values)

Using ur method the values dont reset themselves, but all actors have the same random_value (in this case maxhp) And no its not luck, i re-checked and re-checked and their equal. (:|

The total (initial) HP of each actor start different though, so somehow THAT value works, but the other (maxhp) does not???    ?_?

I think the problem is that I left the value maxhp (NOT base_maxhp) completely alone and have been experimenting with base_maxhp only, but  im at school now, so ill post later when i get home and test this fact. thx agn

pokeball WcWOfflineMale
***
Rep:
Level 87
2 + 2 is a math problem, NOT 4.
How about make the random number a class variable of Game_Actor and let it be referenced instead of
Code: [Select]
rand()
each time? That should do what you want.
If you are reading this, the government of the U.S.A. is currently planting a chip in your brain.
People like decreasing my Rep. In fact, people like decreasing most others people's rep.

*
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 Veteran2011 Favourite Staff Member2011 Most Mature Member2011 Best RPG Maker User (Scripting)2011 Best Use of Avatar and Signature Space2010 Most Mature Member2010 Favourite Staff Member
Well, the solution Zeriab posted does pretty much that. @random_hp_add is in Game_Actor, and it will initialize if it has not already been initialized. My guess would still be that this method:

Code: [Select]
  #--------------------------------------------------------------------------
  # * Get Maximum HP
  #--------------------------------------------------------------------------
  def maxhp
    n = [[base_maxhp + @maxhp_plus, 1].max, 9999].min
    for i in @states
      n *= $data_states[i].maxhp_rate / 100.0
    end
    n = [[Integer(n), 1].max, 9999].min
    return n
  end

needs to be overwritten, but of course I could be wrong.

***
Rep:
Level 87
ya thats wat i thought. thx

***
Rep:
Level 87
bit of a necropost but... originally i had not used zeriabs idea in its fullest and used the self. function, only the class variable idea. Now, I just basically copied down what Z wrote just for good measure, and it says method 'random_hp_add' is undefined...

ps-yes, i did define it as Z wrote it

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
What exactly is the error?
Take a screenshot of the error if you can and post it here

***
Rep:
Level 87
NEVERMIND!!! figured it out :)

originally i kept making @random_hp_add = value, then in the maxhp definition += rand(50) or saying @raandom_hp_add = rand(50), the trick was to define that in the initialization, not the defintion of maxhp. (tho i have a feeling u said that, i just didnt read it right ;)

so, thx Z and the rest a yall 4 ur help  ;8
« Last Edit: December 16, 2007, 08:37:23 PM by da good king »