0

I am struck at writing a java equivalent code for a C code to generate random number. The C code is as follows : static void lng_cosem_CreateRandom(u8 *u8p_Array_p, u8 u8_Len_p) { u8 u8_Indx; u32 u32_Temp;

                srand(GetTickCount());
                #endif /* ABC_COMPILER_USED */
               while( u8_Len_p )
              {
                  u32_Temp = SYS_GetRandom32();
                  for( u8_Indx = 0; u8_Indx < 4; u8_Indx++ )
                  {
                       *u8p_Array_p++ = (u8)u32_Temp;
                        u32_Temp >>= 8;
                        u8_Len_p--;
                        if( !u8_Len_p )
                        {
                                break;
                        }
                  }
            }
      }

I wrote a Java equivalent code to the above as follows :

         public  static void CreateRandom(byte[] COSEM_CTOS, byte   
     COSEM_CHALLENGE_LEN)  
      {                 
             long temp;
             int index;
             pos = 0;
             while(COSEM_CHALLENGE_LEN!=0)
            {
                   Random rand = new Random();
                   temp = Math.abs(rand.nextInt());
                   System.out.println(temp + "absolute val");
                   for(index=0;index<4;index++)
                   {
                        COSEM_CTOS[pos++] = (byte) temp;
                        temp >>= 8;
                        System.out.println(temp + "right shift value");
                        COSEM_CHALLENGE_LEN--;
                        if(COSEM_CHALLENGE_LEN == 0 )
                        {
                             break;
                        }
                    }
             }
         }

But i get negative numbers after the right shift operation.I want only positive numbers. How can i do it ?

Ok,Now i have modified the code.But then i am getting negative random number.Please suggest changes in the code as follows

public  static void CreateRandom(byte[] COSEM_CTOS, byte COSEM_CHALLENGE_LEN) 
{
    ByteBuffer b = ByteBuffer.allocate(4);
    byte[] temp = b.array();
    int index;
    pos = 0;
    while(COSEM_CHALLENGE_LEN!=0)
    {
        Random rand = new Random();
        rand.nextBytes(temp);
        for(index=0;index<4;index++)
        {
            COSEM_CTOS[pos++] = temp[index];
            COSEM_CHALLENGE_LEN--;
            if(COSEM_CHALLENGE_LEN == 0 )
            {
                break;
            }
        }


        /* or we can use Math.abs(rand.nextByte(COSEM_CTOS)); */
    }
}

enter code here
Abinaya
  • 23
  • 7

1 Answers1

0

I don't really understand, what you are trying to accomplish. But the task 'Generate a random natural (positive) number' is well documented over the internet either in C(++) and in Java.

If you want to store the four parts of an 32-bit integer into bytes there must be more elegant ways to implement, than the way you try above. something like here:

ByteBuffer b = ByteBuffer.allocate(4);
b.putInt(temp);

byte[] result = b.array();

(btw, assigning a new Random-Object in every while-iteration is a quite bad idea imho)

Community
  • 1
  • 1
Simon K.
  • 348
  • 1
  • 7
  • 24