Computer Science Canada

Randomization

Author:  tupac [ Tue Mar 28, 2006 2:26 pm ]
Post subject:  Randomization

i was wondering, how would you randomize numbers in visual basic. i tryed to learn different algorithms, but they keep giving me the same number when i run the perogram. anyway, any help will be greatly apreciated

Author:  codemage [ Tue Mar 28, 2006 2:34 pm ]
Post subject: 

Put the following line in your Form_Load:
Randomize

And then you can use
Rand(1, 10) for random integers

or

Rnd for real numbers (that you'll have to scale to suit your purpose).

Author:  tupac [ Tue Mar 28, 2006 2:46 pm ]
Post subject: 

it says "rand" is not a defined function Confused

Author:  GlobeTrotter [ Tue Mar 28, 2006 6:44 pm ]
Post subject: 

codemage, that's not really it. The primary random functions are :

Randomize()
and
Rnd()

Randomize shoudl just go at the beginning to set the seed.
Rnd returns a random real between 0 and 1. Thus, you can create your own function to output a random integer in a given range.

Author:  tupac [ Tue Mar 28, 2006 11:52 pm ]
Post subject: 

nvm, i figured out an algorithm that works.
thnx for all the help Very Happy

Author:  codemage [ Wed Mar 29, 2006 12:33 pm ]
Post subject: 

OMG, my bad. I'm accustomed to using custom modules & libs for too many of these "should be standard" libraries.

My "Rand" function is as follows:

code:

Public Function Rand(Low As Long, High As Long) As Long
  Rand = Int((High - Low + 1) * Rnd) + Low
End Function

Author:  tupac [ Wed Mar 29, 2006 4:04 pm ]
Post subject: 

yea, mine looks something like that, thnx pple Smile


: