Random No generation problems

R

Roy Gourgi

Hi,

How do I invoke the random number generator that was suggested by a few
people. Ideally, what I would like to do is to instantiate the random no.
generator with a seed value that does not repeat the values and that can be
called from any class, as I have to call the random number generator from a
few different classes.
Here is my code:

using System;

class cMain

{

static void Main()

{

}

}



public class cGetRandom

{

static Random rand = new Random();

private static int GetRandom()

{

lock ( rand )

{

return rand.Next();

}

}

}

public class cClass1

{

private static int Class1()

{

int lnRand=0;

lnRand=cGetRandom.rand.Next(1, 49);


}

}

TIA

Roy
 
R

Roy Gourgi

Hi Chris,

Yes I have seen something similar to Random(Environment.TickCount) that is
like this:

Random(unchecked((int)DateTime.Now.Ticks));

but what I want to know is if I have to create an instance of the Random
constructor every time I want to generate a random number in a class. You
see I have to generate random numbers in a few different classes, so ideally
I would have like to just use a constructor only once just as you do in C++
(srand( (unsigned)time( NULL ) ) to get a random number every time and then
always use that same instance to generate a random number. Is that possible?

Thanks
Roy
 
C

Chris Dunaway

Pass Environment.TickCount into the Random constructor. That should
seed the generator and give you the randomness you need.
 
R

Roy Gourgi

Hi Chris,

Yes I have seen something similar to Random(Environment.TickCount) that
is like this:

Random(unchecked((int)DateTime.Now.Ticks));

but what I want to know is if I have to create an instance of the Random
constructor every time I want to generate a random number in a class.
You see I have to generate random numbers in a few different classes, so
ideally I would have like to just use a constructor only once just as
you do in C++ (srand( (unsigned)time( NULL ) ) to get a random number
every time and then always use that same instance to generate a random
number. Is that possible?

Thanks
Roy
 
R

Roy Gourgi

Hi Jon,

I tried calling cGetRandom.GetRandom() but I get the error message that it
is inaccessible because of it's protected nature (i.e. private). How do I do
it then?
I am trying to do what Markus suggested (I am not sure if I did it properly
though), that is to create only 1 instance of the random class. I would like
the instance (rand) to be seen throughout many different classes and not
just 1 class as I have here. So is there a way, to do it so I only
instantiate it once and then can use it in any class.

Here is my code:
using System;

class cMain

{

static void Main()

{

}

}

public class cClass1

{

private static int Class1()

{

int lnRand=0;

lnRand= cGetRandom.GetRandom();

}

}



public class cGetRandom

{

static Random rand = new Random();

private static int GetRandom()

{

lock ( rand )

{

return rand.Next();

}

}

}

Thanks
Roy
 
J

Jon Skeet [C# MVP]

Chris Dunaway said:
Pass Environment.TickCount into the Random constructor. That should
seed the generator and give you the randomness you need.

The parameterless constructor for Random effectively does that already
(it uses a time-dependent seed, anyway).
 
J

Jon Skeet [C# MVP]

Roy Gourgi said:
How do I invoke the random number generator that was suggested by a few
people. Ideally, what I would like to do is to instantiate the random no.
generator with a seed value that does not repeat the values and that can be
called from any class, as I have to call the random number generator from a
few different classes.

You're already doing that, although the sample code you've given
wouldn't compile (you should be calling cGetRandom.GetRandom(), not
cGetRandom.rand.Next()).

In particular, you *shouldn't* create new instances of Random, as
otherwise you could easily get repeated sequences.
 
R

Roy Gourgi

Yes you are right, when I make it public instead of private it works, but I
was just following Markus' code when he mentioned that to make it truly
random you have to create that class.

Yes I definately need a couple of good books one on C# and another on OOP.
Can you suggest any.

My program is coming along fine and I have almost finished converting it to
C#, though I do not think it is the best written code in C#. :)

Thanks by the way.
Roy

Thanks
Roy
 
J

Jon Skeet [C# MVP]

Roy Gourgi said:
I tried calling cGetRandom.GetRandom() but I get the error message that it
is inaccessible because of it's protected nature (i.e. private). How do I do
it then?

You make it public.

I would suggest at this stage that you read a book on C# and another
one on object orientation. Newsgroups are not a good way of picking up
the basics on either of them, excellent as they are for answering more
specific questions when you've got the basics.
I am trying to do what Markus suggested (I am not sure if I did it properly
though), that is to create only 1 instance of the random class. I would like
the instance (rand) to be seen throughout many different classes and not
just 1 class as I have here. So is there a way, to do it so I only
instantiate it once and then can use it in any class.

You're already doing that. You just need to sort out the access to the
GetRandom method.
 
R

Roy Gourgi

Well, it won't be *truly* random anyway - but it's certainly better to
use one instance of Random throughout.

When you say that it is not truly random it is because it uses the system
clock for it's seed value and it is conceivable that if you get the same
clock value as a seed, then it won't truly be random, right? Some food for
thought is that nothing is actually random rather only virtually random
because whatever the event it is nothing more than the product of all the
factors that actually have a bearing on the result. The more factors, the
more the probability that you will not repeat that random sequence.

A couple of things to start with:
1) I don't think I've seen you write any non-static methods yet, which
suggests there's very little object orientation going on.
2) You should really look at the .NET naming guidelines:

What do you mean by non-static methods? I am thinking that maybe one day I
will have someone re-write my program properly. Right now that it is not of
the uptmost priority. But I do believe that the nature of my program does
not lend itself to a fully OOP (I may be wrong) but as you saw in my
timing, I mean checker :) program that OOP would not be that suitable (I may
be wrong). It is strictly computational and most of the computations have to
be carried out in a single function (class) at a time.

When you say to look at the .NET naming guidelines, do you mean the
conventional nomenclature that is used? I think I definately have to get
some books on C# and OOP, that is for sure.

Roy
 
J

Jon Skeet [C# MVP]

Roy Gourgi said:
Yes you are right, when I make it public instead of private it works, but I
was just following Markus' code when he mentioned that to make it truly
random you have to create that class.

Well, it won't be *truly* random anyway - but it's certainly better to
use one instance of Random throughout.
Yes I definately need a couple of good books one on C# and another on OOP.
Can you suggest any.

I'm afraid I can't, really. The only book I used when learning C# was
C# in a Nutshell, and that had quite a few mistakes in it. I'm sure
there *are* plenty of good books out there - I just haven't read many
C# books (at least, not in a "learning" way).
My program is coming along fine and I have almost finished converting it to
C#, though I do not think it is the best written code in C#. :)

A couple of things to start with:
1) I don't think I've seen you write any non-static methods yet, which
suggests there's very little object orientation going on.
2) You should really look at the .NET naming guidelines:
http://tinyurl.com/2cun
 
J

Jon Skeet [C# MVP]

Roy Gourgi said:
When you say that it is not truly random it is because it uses the system
clock for it's seed value and it is conceivable that if you get the same
clock value as a seed, then it won't truly be random, right?

Yes, it's a pseudo-random number generator. Even within the bounds of
PRNGs, it's not *very* random. There's
System.Security.Cryptography.RandomNumberGenerator which gives
cryptographically strong random numbers.
Some food for
thought is that nothing is actually random rather only virtually random
because whatever the event it is nothing more than the product of all the
factors that actually have a bearing on the result. The more factors, the
more the probability that you will not repeat that random sequence.

Well, some processors provide better RNGs which look at things like
processor temperature to give random number sequences which aren't
repeatable. I don't know if there's any way of getting to it using
P/Invoke - but I suspect it's not necessary for what you're doing.
What do you mean by non-static methods?

Instance methods. I don't have enough time to describe OOP to you here
- a book would be a much better idea.
I am thinking that maybe one day I
will have someone re-write my program properly. Right now that it is not of
the uptmost priority. But I do believe that the nature of my program does
not lend itself to a fully OOP (I may be wrong) but as you saw in my
timing, I mean checker :) program that OOP would not be that suitable (I may
be wrong).

I believe you *are* wrong, and that the earlier you start writing code
in an OO way, the better. In my experience, "I'll go back and make it
better" very rarely actually happens.
It is strictly computational and most of the computations have to
be carried out in a single function (class) at a time.

This is half the problem - you don't have to have a single methods in a
single class. Instead, you put all the methods which act on the same
data in the same class.
When you say to look at the .NET naming guidelines, do you mean the
conventional nomenclature that is used? I think I definately have to get
some books on C# and OOP, that is for sure

I mean how to name classes, methods etc.
 
R

Roy Gourgi

I believe you *are* wrong, and that the earlier you start writing code
in an OO way, the better. In my experience, "I'll go back and make it
better" very rarely actually happens.

I totally agree with OOP being much better, but I do not know how to from a
strictly OOP standpoint. I programmed in OOP in VFP and I thought that it
was quite intuitive, but C# is under a fully OOP infrastructure and it is
different. I have declared a class "GlobalVariables" where I put all my
global variables and arrays that I had in C++. This is probably another area
were there is room for improvement.
Instance methods. I don't have enough time to describe OOP to you here
- a book would be a much better idea.

I agree, a book would be much better at this point.
This is half the problem - you don't have to have a single methods in a
single class. Instead, you put all the methods which act on the same
data in the same class.

Yes, this could make sense from a manageability standpoint but I am not sure
that it would make it faster. But yes, this is something that I should do.

Roy
 
J

Jon Skeet [C# MVP]

Roy Gourgi said:
I totally agree with OOP being much better, but I do not know how to from a
strictly OOP standpoint. I programmed in OOP in VFP and I thought that it
was quite intuitive, but C# is under a fully OOP infrastructure and it is
different. I have declared a class "GlobalVariables" where I put all my
global variables and arrays that I had in C++. This is probably another area
were there is room for improvement.

While C# is OO-based, that doesn't stop you from writing code which
really isn't properly OO - such as your code.
I agree, a book would be much better at this point.

Righto - sorry about not being able to give you any reccommendations :(
Yes, this could make sense from a manageability standpoint but I am not sure
that it would make it faster. But yes, this is something that I should do.

It doesn't need to make it faster - it just needs to make it easier to
read without significantly harming performance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top