Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Random Number

Well, I've been trying to puzzle this one out, to no avail. I know that RMXP has a built-in random number function, but I can't seem to make it work properly. I made a script that utilized it in cunjunction with my timer, and nothing would ever happen. So, I decided to make a script that tested the random number function, only to find out that it doesn't appear to create an integer. I was wondering if anyone could tell me exactly how the rand() function works. Thanks for any help I get!
 
I've looked it up in the help book already. What I really need to knwo is why it isn't returning as an integer.

Here's the test script that I had:

Code:
class Numbercheck
  
  def initialize
    number = rand(8)
    if number >=0
      return true
    else
      return false
    end
  end
end

No matter how I change it, it still returns as false.
 

OS

Sponsor

That is strange. It works for me.

Try using this code, and tell me if it works:

Code:
class NumberCheck
  def initialize(max = 8)
    num = rand(max)
    if (num >= 4)
      print "true " + num.to_s
    else
      print "false " + num.to_s
    end
  end
end

n = NumberCheck.new
n = NumberCheck.new(0)
n = NumberCheck.new(10)
n = NumberCheck.new(100)
n = NumberCheck.new(1000)
n = NumberCheck.new(10000)

This code returns correctly. If it does not return correctly for you, than I am not sure what could be wrong. Your code works fine in my RMXP; it always returns true. Just to be sure that it is running correctly, I changed number >= 0 to 4. It works perfectly for me, so go ahead and try it.
 
OS":yfyrj8ux said:
That is strange. It works for me.

Try using this code, and tell me if it works:

Code:
class NumberCheck
  def initialize(max = 8)
    num = rand(max)
    if (num >= 4)
      print "true " + num.to_s
    else
      print "false " + num.to_s
    end
  end
end

n = NumberCheck.new
n = NumberCheck.new(0)
n = NumberCheck.new(10)
n = NumberCheck.new(100)
n = NumberCheck.new(1000)
n = NumberCheck.new(10000)

This code returns correctly. If it does not return correctly for you, than I am not sure what could be wrong. Your code works fine in my RMXP; it always returns true. Just to be sure that it is running correctly, I changed number >= 0 to 4. It works perfectly for me, so go ahead and try it.

Here's the messages it printed for me:

False 2

False 0.0878319763578475

True 6

True 28

True 435

true 7525

I can't help but wonder what was different for me.
 
Actually, it seems to be working fine.

The first one inputs a random number between 0 and 8, so 2 would be false.
The second is a float between 0 and 1.
The third is between 0 and 10.
The fourth, 0 and 100, the fifth 0 and 1000, so on.

The only reason the first two are false is because those numbers are less than 4.
 

Zeriab

Sponsor

Your problem is simply that you return stuff in the initialize method.
NumberCheck.new will return an instance of NumberCheck and not what the initialize method returns.
Try for example this code:
Code:
class Foo
  def initialize
    return true
  end
end


if Foo.new == true
  p 'true'
else
  p 'false'
end

p Foo.new

So you must have a separate method to call the initialize method and return the result before you can use the initialize method for what you are trying to do.
You should instead have made a class method or another instance function for checking what you wanted.

Edit:
Sorry... Didn't look at the date >_<
It shall not happen again
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top