Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,083 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,545 people online right now. Registration is fast and FREE... Join Now!




Code convcersion help needed... plz

 
Reply to this topicStart new topic

Code convcersion help needed... plz, c++ to c# code conversion

anil000
10 Apr, 2008 - 03:11 AM
Post #1

New D.I.C Head
*

Joined: 10 Apr, 2008
Posts: 3

Hi,
I'm trying to convert the code in c++ to c#.
CODE

                int seedP[] = new int[3];
                int seedH[] = new int[3];
                seedP[0] = 1031; seedH[0] = 3191;
    seedP[1] = 2521; seedH[1] = 5639;
    seedP[2] = 9931; seedH[2] = 7583;

void activatePackSource(unsigned long long clockTime) {
        if(erand48(seedP) <= p) {
            numPacketsGenerated++;
            packetGenerated = 1;
            

if((erand48(seedH)*100)<=HOT_PERCENT)tempPacket.address

= HOT_SPOT;
            else                        

tempPacket.address = floorl(drand48()*N);
            tempPacket.tag =

findFabricTag(tempPacket.address)^sourceID;
            tempPacket.distance =

findDistance(tempPacket.tag);
            tempPacket.status =

tempPacket.position = 0;
            tempPacket.entry = sourceID;
            tempPacket.startTime =

clockTime;
        }
        else packetGenerated = 0;
    }



Here, i've used Random class, for generating random numbers, but was unable to pass the speific seeds into the methods like.erand48(seedH).
Can you please suggest me, a way of fixing it.
Thanking you,
Anil
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Code Convcersion Help Needed... Plz
10 Apr, 2008 - 03:21 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8525
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Why don't you start by telling us the exact error message(s) you're receiving
User is online!Profile CardPM
+Quote Post

anil000
RE: Code Convcersion Help Needed... Plz
10 Apr, 2008 - 09:49 AM
Post #3

New D.I.C Head
*

Joined: 10 Apr, 2008
Posts: 3

QUOTE(PsychoCoder @ 10 Apr, 2008 - 04:21 AM) *

Why don't you start by telling us the exact error message(s) you're receiving



Hi,
Thanks for listening to me. Actually, i'm not getting any error messages. The code i've taken in my program for random numbers is
CODE

private void init_genrand(uint s)
        {
        mt[0] = s & 0xffffffffU;
        for (mti = 1; mti < Ng; mti++)
            {
            mt[mti] =
              (uint)(1812433253U * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
            
            // In the previous versions, MSBs of the seed affect  
            // only MSBs of the array mt[].                        
                      
            mt[mti] &= 0xffffffffU;
            // for >32 bit machines
            }
        }

    public uint genrand_int32()
        {
        uint y;
        
        int Ng = 624;
        int mti = Ng + 1;
        int M = 397;
        uint UPPER_MASK = 0x80000000U; // most significant w-r bits
        uint LOWER_MASK = 0x7fffffffU; // least significant r bits
        uint MATRIX_A = 0x9908b0dfU;   // constant vector a
        // mag01[x] = x * MATRIX_A  for x=0,1
        uint[] mag01 = {0x0U, MATRIX_A};
        // the array for the state vector
        uint[] mt = new uint[Ng];*/
        uint y;
        

        if (mti >= Ng)
            { /* generate N words at one time */
            int kk;

            if (mti == Ng + 1)   /* if init_genrand() has not been called, */
                init_genrand(5489U); /* a default initial seed is used */

            for (kk = 0; kk < Ng - M; kk++)
                {
                y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
                mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1U];
                }
            for (; kk < Ng - 1; kk++)
                {
                y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
                mt[kk] = mt[kk + (M - Ng)] ^ (y >> 1) ^ mag01[y & 0x1U];
                }
            y = (mt[Ng - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
            mt[Ng - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1U];

            mti = 0;
            }

        y = mt[mti++];

        // Tempering
        y ^= (y >> 11);
        y ^= (y << 7) & 0x9d2c5680U;
        y ^= (y << 15) & 0xefc60000U;
        y ^= (y >> 18);

        return y;
        }
        public void activatePackSource(ulong parameter1)
        {
           double random_number1 = (((double)genrand_int32()) + 0.5) * (1.0 / 4294967296.0);
            if (random_number1 <= DefineConstants.p)
                {
                numPacketsGenerated++;
                packetGenerated = 1;
        }


My problem is, i want to get a random number between 0.0 to 1.0, by using different seed values. And the Random class supported NextDouble() method is not permitting me to give different seed values, which is very crucial for the program output... for obtaining the accurate result. I'll be very greatful to your new ideas.
Thanking you,
Awaiting your worthy response,
Anil blink.gif
User is offlineProfile CardPM
+Quote Post

skaoth
RE: Code Convcersion Help Needed... Plz
10 Apr, 2008 - 09:55 PM
Post #4

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 342



Thanked: 10 times
Dream Kudos: 100
My Contributions
QUOTE

My problem is, i want to get a random number between 0.0 to 1.0, by using different seed values. And the Random class supported NextDouble() method is not permitting me to give different seed values


Well the Random classes NextDouble() method does return a number between 0 and 1 as the documentation indicates.
Further more you can see the random class, what you can't seed is the NextDouble() method

CODE

class Program
    {
        static void Main(string[] args)
        {
            // Seed the Random number generator
            Random r = new Random(44);
            for (int i = 0; i < 20; i++)
            {
                // Get a random number between 0 and 20
                float f = (float)r.NextDouble() * 20.0f;
                Console.WriteLine("float: {0}", f);
            }

            Console.ReadLine();
        }
}


Is this what you are looking for?
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:46PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month