Mirage Source http://web.miragesource.net/forums/ |
|
Random Function? http://web.miragesource.net/forums/viewtopic.php?f=201&t=4000 |
Page 1 of 2 |
Author: | Pbcrazy [ Thu Jul 17, 2008 7:10 pm ] |
Post subject: | Random Function? |
Ok i was wondering if there is a function or how to make a function where itll pick a random number between a predetermined high and low number? so like youd call Code: Rand(3 'High num, 1 'Low num)
|
Author: | seraphelic [ Thu Jul 17, 2008 7:29 pm ] |
Post subject: | Re: Random Function? |
I'm unfamiliar with all the math functions in vb, but I'm good with theory. I think it should be something like function RandRange(ByVal Low As Long,ByVal High As Long) As Long Rand = int((High - Low + 1) * "random number") + Low end function somethin like that. look up the random function and replace "random number" with it. |
Author: | Dragoons Master [ Thu Jul 17, 2008 7:52 pm ] |
Post subject: | Re: Random Function? |
Code: Dim MyValue
MyValue = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6. |
Author: | seraphelic [ Thu Jul 17, 2008 8:04 pm ] |
Post subject: | Re: Random Function? |
Dragoons Master wrote: Code: Dim MyValue MyValue = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6. That wouldn't work. if Rnd turned out to be .9, then MyValue would equal 7. Here's a working function. Function Rand(ByVal Low As Long, ByVal High As Long) As Long Rand = Int((High - Low + 1) * Rnd) + Low End Function |
Author: | Asrrin29 [ Thu Jul 17, 2008 8:09 pm ] |
Post subject: | Re: Random Function? |
This is what I use in my program, and is standard for creating random numbers within a program. Code: Function Rand(ByVal Low As Integer, ByVal High As Integer) As Integer Randomize Rand = ((High - Low + 1) * Rnd) + Low End Function If you don't call Randomize then the random numbers will be the same every time you start the program. Randomize bases the rand function on a system clock to make it closer to being "truly" random. |
Author: | Pbcrazy [ Thu Jul 17, 2008 8:10 pm ] |
Post subject: | Re: Random Function? |
sweet thanks asrrin (woot i spelled it right ), dragoons master's kinda worked. you could have a high but there wouldnt be a low number to put into it. thanks everyone |
Author: | Dragoons Master [ Thu Jul 17, 2008 9:19 pm ] |
Post subject: | Re: Random Function? |
seraphelic wrote: Dragoons Master wrote: Code: Dim MyValue MyValue = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6. That wouldn't work. if Rnd turned out to be .9, then MyValue would equal 7. Here's a working function. Function Rand(ByVal Low As Long, ByVal High As Long) As Long Rand = Int((High - Low + 1) * Rnd) + Low End Function Actualy... no, Int(6*0,9)+1 = 6 I got that example from MSDN, so it actually can't be wrong... But for any range: Int((upperbound - lowerbound + 1) * Rnd + lowerbound) Also got it from MSDN. I miss read the first example. |
Author: | seraphelic [ Thu Jul 17, 2008 9:50 pm ] |
Post subject: | Re: Random Function? |
My fault, I misinterpreted Int(). |
Author: | Lea [ Thu Jul 17, 2008 11:41 pm ] |
Post subject: | Re: Random Function? |
A six second google returned this: Code: Function Random(Lowerbound As Long, Upperbound As Long) Randomize Random = Int(Rnd * Upperbound) + Lowerbound End Function There are better ways to do it... you do not need to call Randomize every time you want to generate a random number, and the minor overhead of a function call is eliminatable . This function has a minor error, I don't know if you'll ever notice it though. Rnd generates a number [0,1] Code: Function InitRandomNumberGenerator() 'Call this only once Randomize End Function Function Random(Lowerbound As Integer, Upperbound As Integer) as Integer Random = Int(Rnd * (Upperbound - Lowerbound)) + Lowerbound End Function How does this look? |
Author: | Dragoons Master [ Fri Jul 18, 2008 12:48 am ] |
Post subject: | Re: Random Function? |
No return type? |
Author: | GIAKEN [ Fri Jul 18, 2008 1:45 am ] |
Post subject: | Re: Random Function? |
Dave wrote: Function Random(Lowerbound As Integer, Upperbound As Integer) as Integer Dragoons Master wrote: No return type? ? |
Author: | Asrrin29 [ Fri Jul 18, 2008 1:53 am ] |
Post subject: | Re: Random Function? |
Dave wrote: A six second google returned this: Code: Function Random(Lowerbound As Long, Upperbound As Long) Randomize Random = Int(Rnd * Upperbound) + Lowerbound End Function There are better ways to do it... you do not need to call Randomize every time you want to generate a random number, and the minor overhead of a function call is eliminatable . This function has a minor error, I don't know if you'll ever notice it though. Rnd generates a number [0,1] Code: Function InitRandomNumberGenerator() 'Call this only once Randomize End Function Function Random(Lowerbound As Integer, Upperbound As Integer) as Integer Random = Int(Rnd * (Upperbound - Lowerbound)) + Lowerbound End Function How does this look? Why not just stick Randomize in the ServerInit sub then? it seems putting it in it's own sub would also cause some overhead. |
Author: | Dragoons Master [ Fri Jul 18, 2008 3:35 am ] |
Post subject: | Re: Random Function? |
GIAKEN wrote: Dave wrote: Function Random(Lowerbound As Integer, Upperbound As Integer) as Integer Dragoons Master wrote: No return type? ? Not this one: Code: A six second google returned this:
Code: Select all Function Random(Lowerbound As Long, Upperbound As Long) Randomize Random = Int(Rnd * Upperbound) + Lowerbound End Function |
Author: | Lea [ Fri Jul 18, 2008 4:06 am ] |
Post subject: | Re: Random Function? |
I think putting no return type makes it a variant. That's why I added it in my modified function. You can definately put Randomize in the serverinit sub... just wanted to make the function because I didn't know where to put it |
Author: | Pbcrazy [ Fri Jul 18, 2008 5:07 pm ] |
Post subject: | Re: Random Function? |
hmmm mk. well im not using MS. but it works and thats good enough for me. at least for now. |
Page 1 of 2 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |