|
Given you are writing a lottery number generater, there is an easier implmenation to select numbers:
1. Create a list of int, containing the numbers in order you want to select from (e.g. 1-50)
2. Create a random position number (starting in the range 0-49 in my example),
3. Take the value at the random position and remove from the list
4. Reduce the random position range by one as you have removed from the source list
5. Rinse and repeat steps 3 & 4 until you have enough numbers to fill your lottery card...
You can refactor this to work without the list, but it will be harder to understand
|
|
|
|
|
|
The seed is the number that you pass in the constructor. E.g.,
randomGenerator = new Random(1); will result in a different list of "random" numbers than this;
randomGenerator = new Random(2); Enjoy
Why?
|
|
|
|
|
This random in not real random function.
long ago i tried to implement random with a seed based on timer (milliseconds), i don't remember what solution exactly i had, i think it was ATL vector solution. I found somethink that might itnrest you:
Random sort order in C# or LINQ[^]
|
|
|
|
|
|
I came to C# from C++ and an curious as to why they chose to use "\r\n" for newlines instead of "\n" - anyone know?
I hope you realise that hamsters are very creative when it comes to revenge. - Elaine
|
|
|
|
|
\r = carriage return
\n = new line equivalent of line feed.
ie Carriage Return / Line feed.
I *think*!
|
|
|
|
|
Yes but C++ managed with just \n - I'm curious as to why they changed to \r\n in C#.
I hope you realise that hamsters are very creative when it comes to revenge. - Elaine
|
|
|
|
|
It's not language-dependent. A single "\n" is unix-style, but Windows wants a "\r\n" to denote a newline. Mind you that "\n" is also treated as a newline, but it's not the convention.
Meaning that if you search an ASCII-text under Windows for newlines, you'd typical be looking for the "\r\n" combination. If you only replaced the "\n" character, a "\r" would remain.
Whot?
|
|
|
|
|
So C# is just sticking more closely to the "\r\n" convention that C++ did. Fair enough!
I hope you realise that hamsters are very creative when it comes to revenge. - Elaine
|
|
|
|
|
In C++ does \n write a \n or a \r\n on windows systems and \r on OS9 and prior macs. The intent could have been to use \n as an alias for the platform specific newline character </devils advocate>
The European Way of War: Blow your own continent up.
The American Way of War: Go over and help them.
|
|
|
|
|
Text files generally contain several "lines" of text, separated by "linefeeds" (ASCII 10).
On some operating systems, the file will also contain "carriage returns" (ASCII 13).
The developers of the original C library decided that, for portability, the programmer shouldn't need to know the details.
So, in C you just specify '\n' and the operating system will add a '\r' if it chooses. Likewise, when reading a text file, if the file contains '\r\n', the '\r' will not be returned to the program.
And it was good.
But the designers of .net decided otherwise and think that the programmer should know the details, so we usually have to specify the '\r' or use System.Environment.NewLine which is supposed to aid portability (not that they expect .net to be ported), but which will do just the opposite in the end.
In theory, on a Windows system NewLine will equate to "\r\n" and on some other system it may be "\n" or "\r" or who knows what.
The problem, as I see it, is that this won't work on OpenVMS -- OpenVMS supports many types of files, including many types of text files.
On an OpenVMS system, you can have text files with "\r\n", "\n", or "\r" or with Fortran encoding, just about anything -- what will NewLine be set to on OpenVMS if .net gets ported to it? I suspect they will settle on "\n" and let the operating system figure it out -- just like in C.
|
|
|
|
|
Since you've already been told the escape sequence is not language dependent, let me give you a tip that is specific to C#: don't use "\r\n", use Environment.NewLine instead.
Cheers,
Vikram. (Proud to have finally cracked a CCC!)
|
|
|
|
|
Thanks Vikram, I had spotted that one. Two characters in \n to 19 in Environment.NewLine...such is the price of progress...*sigh*
I hope you realise that hamsters are very creative when it comes to revenge. - Elaine
|
|
|
|
|
Worse, try embedding it in a string literal.
string s = "Hello, \nworld!"
is way better than
string s = string.Format ( "Hello, {0}world!" , System.Environment.NewLine ) ;
|
|
|
|
|
I have a list box in my WinForms app that uses up and down buttons to organize the items. What I really would like is to be able to drag the items to the desired position within the list box.
The only information I could find was regarding dragging & dropping to and/or from the list box. Can anyone point me in the right direction for what I'd like to do? Thanks!!!
I should note that this is a .NET 2.0 app
|
|
|
|
|
|
Greetings all,
I am facing a problem in my project.
I created a button that should color the whole row to a red when cloumn " called status" is equal to "open".
As an example:
when column "status" of that row is "OPEN" then the whole row will be red.
When column "status" of that row is "STOP" then the whhole row will be green.
and etc depending on the condition.
that will be checked in the whole database.
Thank you for your help and regards,
|
|
|
|
|
What control are you using ? DataGridView ?
then:
foreach(DataGridViewRow row in dataGridView.Rows)
{
foreach(DataGridViewCell cell in row.Cells)
{
cell.Style.BackColor = Color.Red;
}
}
|
|
|
|
|
Hello,
I'm trying to add Winsock COM component control to my program (like here ),
but i can't find it any where.
It's supposed to be betwwen:
Microsoft Web Browser and Microsoft Works Imaging Server.
Maybe it is at diffrent place in VS 2008?
Someone known?
|
|
|
|
|
Do you have visual Basic 6 runtime installed on your machine?Mswinsck.ocx which is Windows Socket Control is not part of Windows by default.In C# you really needn't to use this component.You have system.net.sockets namespace instread.
Life is a stage and we are all actors!
modified on Wednesday, August 19, 2009 10:05 AM
|
|
|
|
|
Hello!
I am using windows application and I have a datagridview which is i am filling from some datatable and after every 15 second i am fetching data from sql database table and filiing datatable again and then binding that datatable with the same datagridview. for refreshing datagriedview in every 15 second i have used timer control now i want to check if datagridview column call "premium" is less than the value in the newly filled datatable then i want to change that column's cell value to blue b'coz current value is greater than the previous value.
can anyone give me the solution for this....plz send me sample code
thanx in advance
|
|
|
|
|
this is a sample for changing color of all cells in a row :
foreach(DataGridViewRow row in dataGridView.Rows)
{
foreach(DataGridViewCell cell in row.Cells)
{
cell.Style.BackColor = Color.Red;
}
}
|
|
|
|
|
thanx for ur help
but how can i chack if that cell value is less than datatable column value... ie
if((convert.todouble(gv.rows[0].cells["Premium"].value)<(dt.Rows[0]["premium"]))
.......because when i bind datagridview with datatable then both shows the same value so how can i check the condition and where should i do this..
plz give me some sample code...
thanx in advance........
|
|
|
|
|
What do you want to compare ?
If you bind datatable to datagridview, it is the same. What you see in datagridview is the same with what you have in datatable.
Values in datagridview change automatically if you change something in datatable.
|
|
|
|