Click here to Skip to main content
15,913,685 members
Home / Discussions / C#
   

C#

 
AnswerRe: Check Application Pin
Luc Pattyn1-Feb-07 8:23
sitebuilderLuc Pattyn1-Feb-07 8:23 
QuestionGraphic library Pin
Michael O.1-Feb-07 7:22
Michael O.1-Feb-07 7:22 
AnswerRe: Graphic library Pin
Luc Pattyn1-Feb-07 8:26
sitebuilderLuc Pattyn1-Feb-07 8:26 
AnswerRe: Graphic library Pin
Christian Graus1-Feb-07 9:23
protectorChristian Graus1-Feb-07 9:23 
AnswerRe: Graphic library Pin
Michael O.1-Feb-07 12:26
Michael O.1-Feb-07 12:26 
GeneralRe: Graphic library Pin
Christian Graus1-Feb-07 14:39
protectorChristian Graus1-Feb-07 14:39 
Question[C# .NET 2.0 VS 2005] deserializer problem : attributes are null ? [modified] Pin
Fabrice Deshayes aka Xtream1-Feb-07 6:52
Fabrice Deshayes aka Xtream1-Feb-07 6:52 
QuestionSeemingly seperate strings point to same object Pin
crunchi1-Feb-07 6:25
crunchi1-Feb-07 6:25 
I am pretty new to c# and I'm writing my thesis on Bio Sequence Alignment So I have alot to do with generating random strings.

This is a *very cut down version of my problem, it is the shortest snippet that explains my predicament.

namespace MyConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass fred = new MyClass();
            MyClass jim = new MyClass();

            fred.generate();
            jim.generate();

            Console.WriteLine(fred.MyString);
            Console.WriteLine(jim.MyString);

            Console.ReadLine();
        }
    }

    public class MyClass {

        private Random rand;
        public string MyString;

        public MyClass()
        {
            this.rand = new Random();
        }

        public void generate()
        {
            // generate a random string...

            StringBuilder tempString = new StringBuilder();
            for (int i = 0; i < 6; i++)
            {
                tempString.Append(getRandomChar());
            }
            // store it away...
            this.MyString = tempString.ToString();
        }

        private char getRandomChar()
        {
            // return one of the three characters selected randomly

            int num;
            num = getRandomInt(0, 2);
            if (num < 1) { return 'A'; }
            else if (num < 2) { return 'B'; }
            else { return 'C'; }
        }

        private int getRandomInt(int lower, int upper)
        {
            // return an int between the two values...
            if (lower > upper)
            {
                int temp = upper;
                upper = lower;
                lower = temp;
            }
            else if (upper == lower)
            {
                return upper;
            }
            return (rand.Next() % (upper - lower + 1)) + lower;
        }
    }
}


I believe this should print out two completely different strings but in reality it prints out two identical strings.

Why oh why is this happening?

More than a solution to this exact problem I wouldn't mind learning what subtle point of c# is causing this.

Oh, and a solution would be good too...

Cheers.
AnswerRe: Seemingly seperate strings point to same object Pin
Marc Clifton1-Feb-07 6:52
mvaMarc Clifton1-Feb-07 6:52 
AnswerRe: Seemingly seperate strings point to same object Pin
PIEBALDconsult1-Feb-07 7:09
mvePIEBALDconsult1-Feb-07 7:09 
GeneralRe: Seemingly seperate strings point to same object Pin
crunchi1-Feb-07 12:50
crunchi1-Feb-07 12:50 
GeneralRe: Seemingly seperate strings point to same object Pin
Russell Jones1-Feb-07 22:01
Russell Jones1-Feb-07 22:01 
AnswerRe: Seemingly seperate strings point to same object Pin
Judah Gabriel Himango1-Feb-07 7:56
sponsorJudah Gabriel Himango1-Feb-07 7:56 
GeneralRe: Seemingly seperate strings point to same object Pin
crunchi1-Feb-07 12:52
crunchi1-Feb-07 12:52 
GeneralRe: Seemingly seperate strings point to same object Pin
Judah Gabriel Himango1-Feb-07 13:23
sponsorJudah Gabriel Himango1-Feb-07 13:23 
QuestionDataTable.Select() [modified] Pin
lost in transition 1-Feb-07 6:03
lost in transition 1-Feb-07 6:03 
QuestionHow do I select the "Directory" Pin
Eyungwah1-Feb-07 6:01
Eyungwah1-Feb-07 6:01 
QuestionHow do I select the "Directory" Pin
Eyungwah1-Feb-07 6:04
Eyungwah1-Feb-07 6:04 
AnswerRe: How do I select the "Directory" Pin
Luc Pattyn1-Feb-07 6:31
sitebuilderLuc Pattyn1-Feb-07 6:31 
QuestionC# equilavent functionality to a C++ Vector Pin
rishid71-Feb-07 5:15
rishid71-Feb-07 5:15 
AnswerRe: C# equilavent functionality to a C++ Vector Pin
Mircea Puiu1-Feb-07 5:30
Mircea Puiu1-Feb-07 5:30 
AnswerRe: C# equilavent functionality to a C++ Vector Pin
Luc Pattyn1-Feb-07 6:37
sitebuilderLuc Pattyn1-Feb-07 6:37 
AnswerRe: C# equilavent functionality to a C++ Vector Pin
Marc Clifton1-Feb-07 7:13
mvaMarc Clifton1-Feb-07 7:13 
Questionhow to put the gradient effect on the BindingNavigator object's bacground in Windows Forms Pin
Rocky#1-Feb-07 5:11
Rocky#1-Feb-07 5:11 
QuestionControls on programmatically added TabPages Pin
jimkeating1-Feb-07 5:09
jimkeating1-Feb-07 5:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.