Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

536
Visualizações
RandomNumberGenerator GetInt32 not defined

I have to change the random number generation in multiple places in the sourcecode I am working at, to use the RandomNumberGenerator class instead of the Random class for security reasons.

After looking at the ms docs I figured I have to use the Create() method to make an instance of RandomNumberGenerator. Everything is fine, I could use the GetBytes() function without a problem, but I can't seem to be able use the GetInt32() which is used for generating ints. I've first tried calling it from the instance, then I saw in the documentation that it is a static function, so i've tried calling it like so RandomNumberGenerator.GetInt32(256) but it doesn't contain a definition, the only methods that intellisense suggests me are Create, Equals and ReferenceEquals and I am quite confused as to why?

I have the namespace System.Security.Cryptography included.

I've tried googling around to see implementations, or if someone else had the same question but I couldn't find anything relatable.

EDIT: We are using .net framework 4.8 so I've been looking at the wrong documentation and this should be the correct one. This one doesn't contain a definition for GetInt32 or anything like that so now I am not sure what should I use to generate random integers safely.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Given that you are working on a version of the .NET Framework that doesn't support the RandomNumberGenerator.GetInt32() static method, an easy way to get an integer would be to get some random bytes and use BitConverter to convert that to an integer:

byte[] rngBytes = new byte[4];

// This could be replaced by your instance of RandomNumberGenerator
RandomNumberGenerator.Create().GetBytes(rngBytes);

// Convert the bytes into an integer
int myInt = BitConverter.ToInt32(rngBytes);

Note: You should be aware that the given integer myInt could be negative given that this will simply convert the bytes into the equivalent integer value. You could simply drop the leading bit before converting or just do myInt = 0 - myInt; to get a positive.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda