| | |
random number display the same
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
im having problems with the following code
it works fine when i have the sleep in
but without the sleep it writes the same number twice
does this have something to do with the way the computer generates the random number? like based on the computers time?
it works fine when i have the sleep in
but without the sleep it writes the same number twice
does this have something to do with the way the computer generates the random number? like based on the computers time?
C# Syntax (Toggle Plain Text)
using System; using System.Threading; namespace RandomNumber { class RandNum { [STAThread] static void Main(string[] args) { Console.WriteLine("Random number pairs from 1-6 - type \"q\" to quit"); Console.WriteLine("{0}, {1}", GetRandNum(1,6), GetRandNum(1,6)); string p = ""; while(p != "q") { p = Console.ReadLine(); if(p == "") { Console.WriteLine("{0}, {1}", GetRandNum(1,6), GetRandNum(1,6)); } } static int GetRandNum(int iStart, int iEnd) { Random r = new Random(); // //sleeping helps generate random number Thread.Sleep(r.Next(100)); int i = r.Next(iStart, iEnd * iEnd); i = i/iEnd + 1; return i; } } }
Greeting:
That a usual problem with random number, the best way to minimize this to use a seed for the random based on the actual time, something like this:
This way unless you run your program at the exact same hour every time, you`ll have different random numbers each time.
That a usual problem with random number, the best way to minimize this to use a seed for the random based on the actual time, something like this:
C# Syntax (Toggle Plain Text)
DateTime x=DateTime.Now(); float mins=x.Minute; float secs=x.Second; float hour=x.Hour; Random r=new Random((secs*mins)+hour);
This way unless you run your program at the exact same hour every time, you`ll have different random numbers each time.
i fixed it, it was because i was creating 2 new randoms at the same time which gave them both the same seed.
so i just made outside the method
so i just made outside the method
C# Syntax (Toggle Plain Text)
using System; using System.Threading; namespace RandomNumber { class RandNum { private static Random rNum = new Random(); [STAThread] static void Main(string[] args) { int iMin = 1; int iMax = 6; Console.WriteLine("Random number pairs from 1-6 - type \"q\" to quit"); string p = ""; while(p != "q") { if(p == "") { Console.WriteLine("{0}, {1}", rNum.Next(iMin, iMax), rNum.Next(iMin, iMax)); } p = Console.ReadLine(); } } } }
•
•
Join Date: Oct 2006
Posts: 8
Reputation:
Solved Threads: 0
Standard way to create random numbers is to create a Random object in class wide or global scope and use object's Next() method to get next random number.
This article explains this in detail.
This article explains this in detail.
![]() |
Similar Threads
Other Threads in the C# Forum
- Previous Thread: Project Hangs
- Next Thread: HttpWebRequest on port 8000
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format formatting forms function gdi+ httpwebrequest image index input install java label list listbox listener mandelbrot marshalbyrefobject math mouseclick mysql networking object operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






