473,387 Members | 1,791 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,387 software developers and data experts.

Random number is C#

Hi everyone,

I have the following code in my class method

TheSeed = (int)DateTime.Now.Ticks;
Random rndNum = new Random(TheSeed);
RandNum = rndNum.Next(0, TotalRows);
Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" + TotalRows);

My page gets called everytime and these are the values that I'm getting for
TotalRows = 8

RandNum:1 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:7 Low:0 High:8

Does anyone know why C# Random function is generating more 1,0,2,3 much more
frequently than 4,5,6,7,8?

Thank you
Maziar A.
Nov 16 '05 #1
2 1836
Have you tried not seeding it? Or only seed it once?
This works for me:

Random rndNum = new Random();
for (int i = 0; i < 20; i++)
{
int rnd = rndNum.Next(0, 8);
System.Diagnostics.Debug.WriteLine("RandNum:" + rnd + " Low:0 " +
"High:" + 8);
}
RandNum:3 Low:0 High:8
RandNum:6 Low:0 High:8
RandNum:5 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:6 Low:0 High:8
RandNum:7 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:4 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:6 Low:0 High:8
RandNum:5 Low:0 High:8
RandNum:2 Low:0 High:8
HTH;
Eric Cadwell
http://www.origincontrols.com
Nov 16 '05 #2
Maziar Aflatoun <ma***@rogers.com> wrote:
I have the following code in my class method

TheSeed = (int)DateTime.Now.Ticks;
Random rndNum = new Random(TheSeed);
RandNum = rndNum.Next(0, TotalRows);
Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" + TotalRows);

My page gets called everytime and these are the values that I'm getting for
TotalRows = 8


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Note that you should create a single instance of Random and then use
that repeatedly, rather than creating a new one each time - otherwise
you could easily end up creating two with the same seed.

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

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

Similar topics

10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
8
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
2
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
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: 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: 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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.