Topic: Random number help

I'm trying to do something in a game engine. I know you really can't help with the details, but I need help with the syntax more or less.

What I have:

#==============================================================================
# ** Random_Maps
#------------------------------------------------------------------------------
#  - Script will randomize maps for dungeons.
#==============================================================================

class Random_Maps
   
   #=================
   # - Main
   #=================
   def Game_Event
   super
   initialize(MAP001, EV001)
     
     A = rand(2..4)
     
     if (A = 2)
       $game_player.reserve_transfer(MAP001, 036, 023, up)
     else (A = 3)
       $game_player.reserve_transfer(MAP003, 008, 002, down)
     else (A = 4)
       $game_player.reserve_transfer(MAP004, 014, 004, down)
     
   end
   
   #=========================
   # End of first random dungeon event
   #=========================

As you can see I want A (random number 2-4) depending on what number it chose to transfer the player.

Now the syntax looks fine to me, but when I actually run the game it gives me an error on: A =rand(2..4)

For each number, 2, 3, or 4. to Execute the variable

Re: Random number help

Wnat's the error?
I suspect it is related to

if (A = 2)

That should be

if (A == 2)

And the same for 3, and 4 conditions. You are assigning the value of 2 to A in your if condition rather than comparing it to the value of A.
You might also want to look at using the case statement rather than if elses

case A
when 2
  puts "A is 2 between"
when 3
  puts "It's 3"
else
  puts "You gave me #{a} -- I have no idea what to do with that."
end

What version of ruby are you running?
You might want to try

r = Random.new
A = r.rand(2...4)
What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Random number help

Version is: Ruby Game Scripting System 2 (RGSS2)
It is based on RGSS2 so I think some things may be different? But I'm under the impression rand(2...4) will only randomize 2 and 3 excluding 4?.

I shall edit this script to what you have suggested and let you know how it goes.

EDIT:

#==============================================================================
# ** Random_Maps
#------------------------------------------------------------------------------
#  - Script will randomize maps for dungeons.
#==============================================================================

 class Random_Maps
   
   #=================
   # - Main
   #=================
   def Game_Event
   super
   initialize(MAP001, EV001)
     
     A = rand(2..4)
     
     case A
     
     when 2
       puts '$game_player.reserve_transfer(VILLAGE, 036, 023, up)'
     when 3
       puts '$game_player.reserve_transfer(MAP003, 008, 002, down)'
     when 4
       puts '$game_player.reserve_transfer(MAP004, 014, 004, down)'
     
   end
   
   #=========================
   # End of first random dungeon event
   #=========================

This version gives the error:
Script Random_Maps line 16: Syntax  error occurred
Dynamic constant assignment
A = rand(2..4)

Last edited by Bloodmorphed (2013-03-05 20:51:54)

Re: Random number help

Dynamic constant assignment means that you are trying to assign a dynamic value to a constant
A constant is obviously not variable as it's constant smile
Curious as to why you chose to use a constant "A" rather than a variable "a" ?

Try

     a = rand(2..4)
     
     case a

Actually Ruby should have just produced a warning rather than an error so there is obviously some difference between the version of ruby you are using and what I'm used to using

And yes you are correct about 2..4 - use whatever values you need

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Random number help

Yeah I just figured that out. However I'm still pretty sure I'm doing this wrong. I got a reply fromthe Ruby monk people, although they did help it really wasn't the fix I was looking for.

Just for reference purposes:

Hey!
Great to see your attempt. Can you tell me more about what you are trying to achieve? Unfortunately, it isn't readily apparent to me from the code.
But here are some recommendations to make your code follow Ruby standards better:
method names must be snakecase. Like this_is_a_snake_case. The letters are lower case and multiple words are separated by an underscore. So, def Game_Event becomes def game_events
Class names does not have underscores. Words are separated by upper case letters. Like so: class RandomMaps
only constants start with uppercase letters. Normal variable names must always be in snakecase. So, A = rand(2..4) should ideally be something like: move = rand(2..4)
008 is an invalid octal. In Ruby, if you start a number with 0, it assumes it is an octal (http://en.wikipedia.org/wiki/Octal). Octal can only have a range from 0-7. I'm assuming you haven't tried running this code - if you do, it would raise an error.
$game_player is not recommended. It is a global variable. Global variables means global state. A fundamental programming best practice is to avoid unbounded state as much as possible.
I've written an unconsidered snippet of code to try approaching your requirement. I'm trying to approximate your solution as much as possible. The details will be wrong, please bear with me!
class World

  # the intention of these constants are unclear to me. Giving them some arbitrary values.
  VILLAGE = "village"
  MAP003 = "map3"
  MAP004 =  "map4"

  UP = "direction-up"
  DOWN = "direction-down"

  attr_reader :map

  def initialize(map)
    @map = map
  end

  def new_turn
    move = rand(4)

    # I'm not quite sure what this signifies. We can work on this if you elaborate this part.
    case move
      when 2
        map.reserve_transfer(VILLAGE, 36, 23, UP)
      when 3
        map.reserve_transfer(MAP003, 8, 2, DOWN)
      when 4
        map.reserve_transfer(MAP004, 14, 4, DOWN)
    end
  end

end

class Map
  def initialize
  end

  def reserve_transfer(who, x, y, direction)
    puts "Transferred #{who} by #{x}, #{y} in #{direction}"
  end
end

world = World.new(Map.new)
world.new_turn
world.new_turn
world.new_turn
Try copy-pasting the above code to a Ruby file and run it. Or even better, go to http://rubymonk.com/learning/books/1/chapters/5-strings/lessons/31-string-basics, and paste this code in the first example. You can use RubyMonk itself to play around with this code!
Let me know what you think of this, also try to give me a detailed idea of you have in your mind. Hope this helps!

Re: Random number help

Well, it seems like you are being offered all the help you need from people who understand the specific nature of your app far better than I do.
The best advice ?I can give you right now is to go ahead and reply with a detailed explanation of what you are trying to achieve within the case statement and see what they come back with

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)