Uniformly random numbers ∈[min, max]

One may also be interested in std::random_shuffle and others. That’s for C++. Read more here. Below you will find C and C++ code.

randomRange.c

C++11 now has it’s built-in generator uniform_int_distribution! Below is an example.

#include <iostream>
#include <random>

std::default_random_engine generator((unsigned int)time(0));
int random(int n) {
  std::uniform_int_distribution<int> distribution(0, n);
  return distribution(generator);
}
int main() {
        for(int i = 0; i < 15; ++i)
                std::cout << random(5) << " " << random(5)<< std::endl;
        return 0;
}

For more info, click here.
There also other distributions supported by C++11, like Bernoulli’s.

One could also use the std::uniform_int_distribution in combination with mt19937, in order to get more speed and quality. Below a working example.

#include <random>
std::mt19937 rng(time(NULL));
int random(int n) {
  std::uniform_int_distribution<int> distribution(0, n);
  return distribution(rng);
}

Check this Stackoverflow C++ Calling Pseudo-Random Number Generator Function within a Loop – Constant Seed Issue too. Of course, std::mt19937 is also C++11.

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

/* This solution only works when N      */
/* evenly divides into RAND_MAX.        */
/* So, the results are not equiprobably */
int randomRange(int min, int max)
{
    return ( rand() % ( max - min + 1 ) ) + min;
}

int main(void)
{
    int i=0;
    srand( (unsigned int)time ( NULL ) );
    while(i++!=10)
        printf("%d\n", randomRange(0,2));
    return 0;
}

This code was developed by me, G. Samaras.
This code is just to get pseudorandom numbers.

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

int main(int argc, char *argv[])
{
	int i, n, seed;
	if (argc < 2)
		return 1;
	n = atoi(argv[1]);
	if (argc > 2)
		seed = atoi(argv[2]);
	else
		seed = time(NULL);
	srand(seed);
	for (i = 0 ; i < n ; i++)
	   printf("%d.\n", rand() % (n+1));
	return 0;
}

However, as prelude says, time_t is a restricted type, and may not be meaningfully converted to unsigned int. That is not a terribly big issue as I have yet to see a system where it would fail to work correctly, and I do not know anyone who has either. But subtle lingering portability issues should leave a sour taste in the mouth. Fortunately, there is a way to use the result of time() portably as a seed for rand(); just hash the bytes of a time_t:

unsigned time_seed()
{
    time_t now = time ( 0 );
    unsigned char *p = (unsigned char *)&now;
    unsigned seed = 0;
    size_t i;

    for ( i = 0; i < sizeof now; i++ )
       seed = seed * ( UCHAR_MAX + 2U ) + p[i];

    return seed;
}
srand ( time_seed() );

Thanks to anduril462, from Long Beach, Ca, for fixing a bug in the code.🙂

A hash will take advantage of the way the system time changes. Even better, the C and C++ standards guarantee that type punning is a portable operation for simple types, and time_t is a simple type. So hashing the system time and seeding rand() with it is a portable solution with desirable properties.

In order to produce random points in Uniform distribution.

int RandomUniform(int n) {  
  int top = ((((RAND_MAX - n) + 1) / n) * n - 1) + n;  
  int r;  
  do {  
    r = rand();  
  } while (r > top);  
  return (r % n);  
}

For a closer look of rand, click here.
Have questions about this code? Comments? Did you find a bug? Let me know!😀
Page created by G. (George) Samaras (DIT)

5 thoughts on “Uniformly random numbers ∈[min, max]

  1. Pingback: random number from interval [0,1]

  2. Pingback: Question on Homework

  3. Pingback: Issue with random numbers

  4. Pingback: Help with this code please!

  5. Pingback: Battleship program. Randomly place ship function help

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s