Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a random byte stream

Tags:

c

I'm testing a network program that sends packet to a remote server using UDP/TCP, and for that, I want to generate some random byte streams.

Here's the function:

unsigned char *gen_rdm_bytestream(int num_bytes)
{
    unsigned char *stream = malloc(num_bytes);
    /*
     * here how to generate?
     */
    return stream;
}
like image 547
user138126 Avatar asked Mar 25 '13 18:03

user138126


People also ask

How to generate random byte?

The java. util. Random. nextBytes() method generates random bytes and provides it to the user defined byte array.

What is a random byte?

random_bytes(int $length ): string. Generates an arbitrary length string of cryptographic random bytes that are suitable for cryptographic use, such as when generating salts, keys or initialization vectors.

How do you generate random bytes in C++?

rand() is used to generate random number within a range. Here min_n is the minimum range of the random numbers and max_n is the maximum range of the numbers. So rand() will return the random numbers between min_n to (max_n – 1) inclusive of the limit values.

How do you generate random bytes in Python?

urandom() method is used to generate a string of size random bytes suitable for cryptographic use or we can say this method generates a string containing random characters. Return Value: This method returns a string which represents random bytes suitable for cryptographic use.


4 Answers

Yes, you have int rand (void); function in C,

Returns a pseudo-random integral number in the range between 0 and RAND_MAX. RAND_MAX is a constant defined in <cstdlib>.

This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand().

EDIT:

As you are commenting, I have written a code for you that may help you to demonstrate, How to use rand(), the program and its output are:

#include <stdio.h>     
#include <stdlib.h>  /* srand, rand */
#include <time.h>    
int main (){
 int i=0;
 srand (time(NULL));
 printf("Five rand numbers: \n");
 for(i = 1; i <= 5; i++){
   printf("\n %d", rand());
 }
 printf("Five rand numbersb between 2 to 5: \n");
 for(i = 1; i <= 5; i++){
   printf("\n %d", (2 + rand()%4));
 }    
 return 1;
} 

Output:

 Five rand numbers: 

 1482376850
 1746468296
 1429725746
 595545676
 1544987577

 Five rand numbers, between 2 to 5: 

 2
 5
 3
 4
 3

Codepad Link

like image 67
Grijesh Chauhan Avatar answered Oct 20 '22 20:10

Grijesh Chauhan


For each byte, you can call a random number generator function. The C standard provides the function rand. Before using it, you should initialize the random sequence with a call to srand.

gen_rdm_bytestream may then look something like that:

#include <stdlib.h>
#include <time.h>

unsigned char *gen_rdm_bytestream (size_t num_bytes)
{
  unsigned char *stream = malloc (num_bytes);
  size_t i;

  for (i = 0; i < num_bytes; i++)
  {
    stream[i] = rand ();
  }

  return stream;
}

srand ((unsigned int) time (NULL));

Since stream is unsigned, if the value returned by rand is greater than UCHAR_MAX, she will be reduced (modulo UCHAR_MAX). Therefore you will get pseudo-random numbers between 0 and 255.

like image 36
md5 Avatar answered Oct 20 '22 18:10

md5


Here's a general function for producing random integer values in a given range:

#include <stdlib>

/**
 * Assumes srand has already been called
 */
int randInRange( int min, int max )
{
  double scale = 1.0 / (RAND_MAX + 1);
  double range = max - min + 1;
  return min + (int) ( rand() * scale * range );
}

Leveraging this to create unsigned char values:

u_char randomByte()
{
  return (u_char) randInRange( 0, 255 );
}

So,

for ( i = 0; i < numBytes; i++ )
  stream[i] = randomByte();
like image 1
John Bode Avatar answered Oct 20 '22 19:10

John Bode


On UNIX, there is the getrandom system call.

#include <sys/random.h>
#include <stdlib.h>

unsigned char * gen_rdm_bytestream(size_t const num_bytes) {
    unsigned char * const stream = malloc(num_bytes);
    getrandom(stream, num_bytes, 0);
    return stream;
}
like image 1
nog642 Avatar answered Oct 20 '22 19:10

nog642