Code convcersion help needed... plz

c++ to c# code conversion

Page 1 of 1

3 Replies - 919 Views - Last Post: 10 April 2008 - 10:55 PM Rate Topic: -----

#1 anil000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-April 08

Code convcersion help needed... plz

Post icon  Posted 10 April 2008 - 04:11 AM

Hi,
I'm trying to convert the code in c++ to c#.
				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

Is This A Good Question/Topic? 0
  • +

Replies To: Code convcersion help needed... plz

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Code convcersion help needed... plz

Posted 10 April 2008 - 04:21 AM

Why don't you start by telling us the exact error message(s) you're receiving
Was This Post Helpful? 0
  • +
  • -

#3 anil000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-April 08

Re: Code convcersion help needed... plz

Posted 10 April 2008 - 10:49 AM

View PostPsychoCoder, on 10 Apr, 2008 - 04:21 AM, said:

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
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:
Was This Post Helpful? 0
  • +
  • -

#4 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: Code convcersion help needed... plz

Posted 10 April 2008 - 10:55 PM

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

 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1