Kimimaru Posted July 28, 2010 Author Share Posted July 28, 2010 I was setting a random value in Eclipse Origins, and it somehow extended beyond my specified range. I've changed my Random function to look like this:```Function Random(ByVal Low As Integer, ByVal High As Integer) As Integer Randomize Random = ((High - Low + 1) * Rnd) + LowEnd Function```This is my randomization code:```Dim RandomNum as IntegerRandomNum = Random(1, 4)```Somehow, I'm getting 5 as a result occasionally. Any ideas? I've also tried this as well:```RandomNum = Int(Random(1, 4))``` Link to comment Share on other sites More sharing options...
jcsnider Posted July 28, 2010 Share Posted July 28, 2010 lol easy… just add function```Function Randomize(ByVal Low As Integer, ByVal High As Integer) As Integer high = high - 1 randomize = Random(low,high)End Function```That should work :/ Link to comment Share on other sites More sharing options...
Robin Posted July 28, 2010 Share Posted July 28, 2010 Why would you make a function which just changes a variable? >_>Not to mention 'Randomize' is already taken.You can just put 'High = High - 1' above 'Random = ((High - Low + 1) * Rnd) + Low'.No idea what's causing it to return '5' though. Link to comment Share on other sites More sharing options...
Kimimaru Posted July 28, 2010 Author Share Posted July 28, 2010 Okay, thanks. I'll adapt to it, but I just thought I'd post it to help out others as well. The Eclipse Evolution 2.7 Rand function works well for me, so maybe that can be a suitable replacement for the current function, if changed to return an Integer instead of a Long? Link to comment Share on other sites More sharing options...
Kimimaru Posted July 28, 2010 Author Share Posted July 28, 2010 Sorry for the double post, but I thought I'd bring some more attention to this, as it can be pretty important. I've found out the reason for the flaw in the Random function. Take a look at the original function, with the randomization fix included:```Function Random(ByVal Low As Integer, ByVal High As Integer) As Integer Randomize Random = ((High - Low + 1) * Rnd) + LowEnd Function```Here, **Random** is clearly an Integer, meaning it rounds to a whole number value. However, you're letting the Random do the rounding itself, and when an integer rounds, it rounds either up or down depending on what number is in the tenths place. So, let's use my random code as an example:```RandomNum = Random(1, 4)```This is what could be happening inside the Random function:```Function Random(ByVal Low As Integer, ByVal High As Integer) As Integer Randomize Random = ((4 - 1 + 1) * .94) + 1End Function```The first part, **((4 - 1 + 1) * .94)**, would yield **3.76** as a value. Add one to that, and it becomes **4.76**. Since you're allowing the integer (Random) to round by itself, it rounds that number up to 5.Now let's see what would happen if you were using a slightly modified Random function that I made:```Function Random(ByVal Low As Integer, ByVal High As Integer) As Integer Randomize Random = Int((High - Low + 1) * Rnd) + LowEnd Function```Notice that the Int() modifier was added. This rounds the number down regardless, so let's take a look at how that fixes the problem, with the same example of course:```Function Random(ByVal Low As Integer, ByVal High As Integer) As Integer Randomize Random = Int((4 - 1 + 1) * .94) + 1End Function```Again, the first part, **((4 - 1 + 1) * .94)**, yields **3.76** as the value. This time, however, the Int() modifier rounds that down to 3\. Adding the 1 to it effectively keeps it within the range, at a value of 4. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now