473,788 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

random always the same.

ad

I write a function below the generate a alpha, but when I call it , it
always return 'b'
How can I do generate a character randomly.
public static char CharRnd()
{
Random rnd = new Random();

char c = (char)rnd.Next( 'a', 'z' + 1);
return c;
}
Dec 3 '06 #1
6 13085
you must set seed. change the code as follows.
public static char CharRnd()
{
// Random rnd = new Random();

Random rd = new Random(DateTime .Now.Ticks);

char c = (char)rnd.Next( 'a', 'z' + 1);
return c;
}
Dec 3 '06 #2
JR
place rd at the class level - don't make a new one each time.

JR
"xhy_China" <xu*********@gm ail.comwrote in message
news:11******** *************@j 44g2000cwa.goog legroups.com...
you must set seed. change the code as follows.
> public static char CharRnd()
{
// Random rnd = new Random();

Random rd = new Random(DateTime .Now.Ticks);

> char c = (char)rnd.Next( 'a', 'z' + 1);
return c;
}

Dec 3 '06 #3
xhy_China <xu*********@gm ail.comwrote:
you must set seed. change the code as follows.
public static char CharRnd()
{
// Random rnd = new Random();

Random rd = new Random(DateTime .Now.Ticks);
The parameterless constructor for Random already uses the current
date/time as a seed. The key is to create a single instance of Random
and use that repeatedly.

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

"Jon Skeet [C# MVP]" <sk***@pobox.co mskrev i en meddelelse
news:MP******** *************** *@msnews.micros oft.com...
xhy_China <xu*********@gm ail.comwrote:
>you must set seed. change the code as follows.
public static char CharRnd()
{
// Random rnd = new Random();

Random rd = new Random(DateTime .Now.Ticks);

The parameterless constructor for Random already uses the current
date/time as a seed. The key is to create a single instance of Random
and use that repeatedly.
But why would the sequence of random numbers be the same for each instance
of Random? Does that mean the OP instantiated all his Random objects
simultaneously (and they therefore had the same date-time seed)?
Dec 4 '06 #5
"Peter Kirk" <pk@alpha-solutions.dksch rieb im Newsbeitrag
news:e9******** ******@TK2MSFTN GP06.phx.gbl...
>
"Jon Skeet [C# MVP]" <sk***@pobox.co mskrev i en meddelelse
news:MP******** *************** *@msnews.micros oft.com...
>xhy_China <xu*********@gm ail.comwrote:
>>you must set seed. change the code as follows.

public static char CharRnd()
{
// Random rnd = new Random();

Random rd = new Random(DateTime .Now.Ticks);

The parameterless constructor for Random already uses the current
date/time as a seed. The key is to create a single instance of Random
and use that repeatedly.

But why would the sequence of random numbers be the same for each instance
of Random? Does that mean the OP instantiated all his Random objects
simultaneously (and they therefore had the same date-time seed)?
Or within 100 ns (= 1 Tick) ;). In the time of GHz-proceccors this should be
possible (depending on what he does in the meantime). The resolution of
DateTime.Now is even less. Wich resolution the parameterless constructor for
Random uses i don't know, but I suppose it's one of these values.
Dec 4 '06 #6
Peter Kirk <pk@alpha-solutions.dkwro te:
The parameterless constructor for Random already uses the current
date/time as a seed. The key is to create a single instance of Random
and use that repeatedly.

But why would the sequence of random numbers be the same for each instance
of Random? Does that mean the OP instantiated all his Random objects
simultaneously (and they therefore had the same date-time seed)?
"Simultaneously " can be pretty loose - it really means "within the time
period it takes for DateTime.Now to change" which can be a long time in
computing terms.

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

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

Similar topics

10
2506
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 spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
8
1905
by: jon morgan | last post by:
OK, I'm going to be brave. There is a bug in VS.Net 1.1 that causes random compiler errors. I have raised this issue in posts at least three time in the past couple of months without attracting much interest. But it's driving me nuts. Here's what happens. I'm working on a multi project VB app. happily writing nice inoffensive code - go to compile and the compiler tells me there's a problem in a project I'm not working on. But really...
25
2982
by: JNY | last post by:
I am using random to generate random numbers, thus: int x,y; for (y = 0;y < 5;y++) { x = random(50); cout << x; }
5
2217
by: user | last post by:
Hello i call it in almost same time (from two different threads): Random rand = new Random(); id=rand.Next(65535); and i often receive the same results. Is it possible ? How windows count this random ? Thanx
11
1843
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using System.Threading; using System.Web.Services;
5
2401
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they can go down similar paths, but not be the same. I will implement code later to make sure i they go more than 10 apart from each other that they are moved closer together, but this is what I have so far, and when the program is run, the two random...
3
1800
by: George Durzi | last post by:
How can I choose a random integer between to given integers? For example, I'd like to pick a random integer between 1 and 3. E.g. 1, 2, or 3 If I do Dim oRandom As New Random(1) Response.Write oRandom.Next(3)
8
5694
by: Aspidus | last post by:
Hi, I wrote a function that generates a ulong I use it 3 times, one after the other, but it generates always the same numbers. At the end i get always 3 equal arrays. Any idea?
26
7916
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand()); int r2 = rand(); bool f = r1 == r2;
16
2084
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10173
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.