473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Random No generation problems

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
Nov 17 '05 #1
13 3591
Pass Environment.TickCount into the Random constructor. That should
seed the generator and give you the randomness you need.

Nov 17 '05 #2
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

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Pass Environment.TickCount into the Random constructor. That should
seed the generator and give you the randomness you need.

Nov 17 '05 #3

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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4
Chris Dunaway <du******@gmail.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Roy Gourgi <ro***@videotron.ca> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Roy Gourgi <ro***@videotron.ca> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7
Roy Gourgi <ro***@videotron.ca> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Roy Gourgi <ro***@videotron.ca> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #9
Roy Gourgi <ro***@videotron.ca> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
> 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
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Roy Gourgi <ro***@videotron.ca> wrote:
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.


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#. :)


http://tinyurl.com/2cun

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #11
Roy Gourgi <ro***@videotron.ca> wrote:
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?


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.
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?


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12
> 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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Roy Gourgi <ro***@videotron.ca> wrote:
> 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?


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.
> 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?


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #13
Roy Gourgi <ro***@videotron.ca> wrote:
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.


While C# is OO-based, that doesn't stop you from writing code which
really isn't properly OO - such as your code.
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.


Righto - sorry about not being able to give you any reccommendations :(
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.


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
10
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
0
by: Adam Carpenter | last post by:
Hello, I am having some problems with these functions which are to be part of the forgotten password system for a website. I am sure it is something simple but I can't see it. I would be...
4
by: Dimos | last post by:
Hello All, I need some help with random number generation. What I need exactly is: To create a few thousand numbers, decimal and integers, between 5 and 90, and then to export them as a...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
1
by: Jonathan Wilson | last post by:
I have a closed source app. I have a .dll plugin for this app (which I am writing). This plugin contains a bug somewhere which seems to clobber memory in a "random" fashion (as in, its not...
16
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.