Java 生成正随机数并右移8位并存储在一个字节中

Java 生成正随机数并右移8位并存储在一个字节中,java,random,unsigned-long-long-int,Java,Random,Unsigned Long Long Int,我对为C代码编写java等效代码以生成随机数感到震惊。 C代码如下: 静态无效lng\u cosem\u CreateRandom(u8*u8p\u数组\u p,u8 u8\u列\u p) { u8 u8_Indx; u32 u32_温度 srand(GetTickCount()); #endif /* ABC_COMPILER_USED */ while( u8_Len_p )

我对为C代码编写java等效代码以生成随机数感到震惊。 C代码如下: 静态无效lng\u cosem\u CreateRandom(u8*u8p\u数组\u p,u8 u8\u列\u p) { u8 u8_Indx; u32 u32_温度

                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;
                        }
                  }
            }
      }
srand(GetTickCount());
#endif/*ABC_编译器_使用*/
而(u8_Len_p)
{
u32_Temp=SYS_GetRandom32();
对于(u8_Indx=0;u8_Indx<4;u8_Indx++)
{
*u8p_数组_p++=(u8)u32_温度;
u32_温度>>=8;
u8_Len_p--;
如果(!u8_Len_p)
{
打破
}
}
}
}
我为上述内容编写了Java等效代码,如下所示:

         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;
                        }
                    }
             }
         }
publicstaticvoidcreaterandom(byte[]COSEM\u CTOS,byte
COSEM_挑战赛(德国)
{                 
长温;
整数指数;
pos=0;
while(COSEM\u CHALLENGE\u LEN!=0)
{
Random rand=新的Random();
temp=Math.abs(rand.nextInt());
系统输出打印项次(温度+绝对值);
对于(索引=0;索引>=8;
系统输出打印项次(温度+右移值);
COSEM_CHALLENGE_LEN--;
if(COSEM\u CHALLENGE\u LEN==0)
{
打破
}
}
}
}
但右移操作后我得到负数。我只想要正数。我怎么做

好的,现在我已经修改了代码。但是我得到了负随机数。请建议对代码进行如下更改

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
public static void CreateRandom(字节[]COSEM\u CTOS,字节COSEM\u CHALLENGE\u LEN)
{
ByteBuffer b=ByteBuffer.allocate(4);
字节[]温度=b.数组();
整数指数;
pos=0;
while(COSEM\u CHALLENGE\u LEN!=0)
{
Random rand=新的Random();
兰特次字节(温度);

对于(index=0;index我真的不太明白,你想完成什么。但是“生成一个随机自然(正)数”的任务在互联网上用C++和Java都有很好的文档记录

如果要将32位整数的四个部分存储到字节中,必须有比上面尝试的更优雅的实现方法。 比如:


(顺便说一句,在每次迭代中分配一个新的随机对象是一个非常糟糕的主意,imho)

你在C代码中使用无符号变量,而在Java代码中使用有符号变量,我知道……但是你能帮我在Java中转换相同的C代码吗?@mgokgoz那么我怎么做呢?Java中的无符号数?
ByteBuffer b = ByteBuffer.allocate(4);
b.putInt(temp);

byte[] result = b.array();