Results 1 to 1 of 1

Thread: Random Numbers

  1. #1
    Super Moderator
    Join Date
    Dec 03
    Location
    Manchester, UK
    Posts
    4,787

    Random Numbers

    Here is another class I put together, I wanted to get a feel for random numbers in .Net, It uses the System.Random Method. Explination Below.

    I will first cover random numbers and then explain how to use the class I produced.


    Random Numbers


    First we must set up our random function, to do this we do the following.

    VB Code:
    1. Private myRandom As New System.Random(Seed)


    Now computers cant produce truly random numbers instead they use a method which in most cases is fairly easy to work out. So to increase how random the numbers actually appear we must place a seed in. In my example I have used the system clock, so somthing like this will work.

    VB Code:
    1. Private myRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))


    Now we have our random function set up, how do we use it? well there are 3 random functions included in .Net,

    Ok to start with we will look at .Next, With this routine we can specify a minimum and maximum value. Very handy


    VB Code:
    1. myRandom.Next(Min, Max)


    So if we wanted to produce a number between 2 certain values we can do that very easily. Note, the max value should be plus 1 of the actual value you want, IE 1 to 10 max value would be 11

    The next method is .NextDouble which will generate a number between 0 and 1.


    VB Code:
    1. myRandom.NextDouble


    Not much to say about this function really, very simple to use

    The final method is .NextBytes which will fill a specified array full of random numbers.


    VB Code:
    1. myRandom.NextBytes(myByteArray)


    Where myByteArray is the array you wish to fill.

    And thats it, thats my brief overview of random numbers in .NET. If there are any corrections here (I am new to .Net) please PM me rather than posting keep this thread clean, and any questions please post in the general forum!

    Ok to help me learn Random number I wrote a very simple class named iRandom which covers random numbers,


    iRandom Class


    To start with we must declare the class,

    VB Code:
    1. Private myRandom as New iRandom


    ok the class covers he 3 main functions,

    To generate a number based on a min and max value simple do the following


    VB Code:
    1. myRandom.GenerateNumber(Min,Max)
    2. messagebox.show CType(myRandom.ReturnNumber,String)


    To return a value between 0 and 1, we use the generateFloat method

    VB Code:
    1. myRandom.GenerateFloat()
    2. messagebox.show CType(myRandom.ReturnNumber,String)


    And finally to fill an array with random numbers,

    VB Code:
    1. myrandom.generateArray(ArraySize)
    2. myArray = myrandom.returnArray()


    Where array size would be the number of slots in the array!

    Ok and thats about it, hope this helps some people out

    I'd just like to mention once again, please do not reply to this. Please PM me use the rating system or post questions regarding numbers in the main forum

    Thanks

    Pino
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •