Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

535
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda