[LINUX] [C language] I want to generate random numbers in the specified range

Introduction

You may want to fill a certain array with an appropriate value. Or you may want to create a random value for your test data. Here are some tips that you can use in such cases.

It is a little inconvenient if rand () / rand_r () is left as it is

Rand () / rand_r () is provided as a function to generate random numbers.

#include <stdlib.h>

int rand(void);
int rand_r(unsigned int *seedp);

However, the above function uses random numbers between 0 and RAND_MAX ** (* 1) **, so if you want to generate random numbers from 0 to UCHAR_MAX (255), you cannot use it as it is. ..

** (* 1) ** In my environment, it was defined in stdlib.h as follows

/* The largest number rand will return (same as INT_MAX).  */
#define	RAND_MAX	2147483647

solution

If you prepare the following wrapper function, you can specify the range of minimum / maximum value (0 to RAND_MAX).

#include <stdlib.h>

int getrand(unsigned int* seed, int min, int max)
{
    return min + (int)(rand_r(seed) * (max - min + 1.0) / (1.0 + RAND_MAX));
}

DEMO

Below is a simple sample program. Please use it to check the operation.

https://github.com/ydah/specified_range_rand_r

Recommended Posts

[C language] I want to generate random numbers in the specified range
I tried to illustrate the time and time in C language
I want to display the progress in Python!
Even in JavaScript, I want to see Python `range ()`!
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I want to extract only pods with the specified label using Label Selector in Client-go
I want to make the Dictionary type in the List unique
[Introduction to Pytorch] I want to generate sentences in news articles
I want to align the significant figures in the Numpy array
I didn't want to write the AWS key in the program
[Linux] I want to know the date when the user logged in
LINEbot development, I want to check the operation in the local environment
I want to make the second line the column name in pandas
I want to pass the G test in one month Day 1
I want to know the population of each country in the world.
I want to pin Spyder to the taskbar
I want to output to the console coolly
I want to print in a comprehension
I want to handle the rhyme part1
I want to handle the rhyme part3
I wrote the selection sort in C
I want to display the progress bar
Generate C language from S-expressions in Python
I want to embed Matplotlib in PySimpleGUI
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
I want to change the color by clicking the scatter point in matplotlib
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to sort a list in the order of other lists
I want to use the Django Debug Toolbar in my Ajax application
I want to leave an arbitrary command in the command history of Shell
I want to visualize where and how many people are in the factory
How to use the C library in Python
I want to do Dunnett's test in Python
How to generate permutations in Python and C ++
I want to pin Datetime.now in Django tests
[Django memo] I want to set the login user information in the form in advance
How to multi-process exclusive control in C language
I want to handle the rhyme part7 (BOW)
I want to store DB information in list
I want to use PyTorch to generate something like the lyrics of Japari Park
I want to merge nested dicts in Python
I want to set a life cycle in the task definition of ECS
I want to see a list of WebDAV files in the Requests module
I made a module in C language to filter images loaded by Python
I wrote the hexagonal architecture in go language
I want to store the result of% time, %% time, etc. in an object (variable)
I tried to generate a random character string
I want to get the file name, line number, and function name in Python 3.4
I want to make C ++ code from Python code!
How to count numbers in a specific range
I want to customize the appearance of zabbix
I want to use the activation function Mish
How to display the modification date of a file in C language up to nanoseconds
I just want to find the 95% confidence interval for the difference in population ratios in Python
I want to replace the variables in the python template file and mass-produce it in another file.
How to limit the API to be published in the C language shared library of Linux
I want to add my own structure to the structure created by Python's C extension module!