Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Actual Random number generation

Allan#Allan# Posts: 2Member
I would like to generate a random number.
Currently im using the folowing:

using System;

private void GeraPadraoProjetado()
{
int i;
int seed = 2; //chosen randomly by me
Random fixRand = new Random(seed);

for(i=0;i<300;i++)
{
PadraoProjetado[i] = (int)(256*fixRand.NextDouble());
}
}

As I have observed, the generated numbers are always the same each time I execute the program.

When I programed in Visual Basic, I remember we had to use the "Randomize()" function before using the function that generated the random numbers in order to essure that the numbers would be actualy random.

Does anyone know what to do in C#?
Thanks in advance!]

Ps.: Excuse me about the code indentation, I simply didnt know how to do it here.



Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    I usually use the the Environment.TickCount property because it is a constantly changing number:

    [code]
    using System;

    private void GeraPadraoProjetado()
    {
    int i;
    Random fixRand = new Random(Environment.TickCount);

    for(i=0;i<300;i++)
    {
    PadraoProjetado[i] = (int)(256*fixRand.NextDouble());
    }
    }
    [/code]
  • Allan#Allan# Posts: 2Member
    Thanks for the help!
    I will use it!

Sign In or Register to comment.