This question already has answers here:
Why does rand() always return the same value? [duplicate]
(2 answers)
Closed 7 years ago.
I have a big problem , I do not understand.
I need to generate random numbers from the interval.
I am using code:
unsigned int nahodnyCisloZIntervalu(unsigned int min, unsigned int max) {
int r;
const unsigned int range = 1 + max - min;
const unsigned int buckets = RAND_MAX / range;
const unsigned int limit = buckets * range;
do {
r = rand();
} while (r >= limit);
return min + (r / buckets);
}
But every time you start the program generates the same numbers!
How to generate truly random numbers usnig C?
Use the standard rand function. It is normal that rand returns the same sequence each time.
Use the srand function at the beginning of your program in order to initialize the random number generator with a "seed", using for example the current time as seed.
Related
This question already has answers here:
How to concatenate two integers in C
(10 answers)
Closed 5 years ago.
Currently I am having a speed bump with adding two different integers. For example
int i = 32;
int j = 50;
/* Add i and j together into 3250 */
What I thought was changing the integers into strings and add them together but that takes too much effort. Is there any other way?
The solution in the decimal system is:
int result = 100* i + j;
In case this should be generic you'll need the following algorithm:
int shift = 10;
while(j >= shift) {
pow *= 10;
}
int result = i * pow + j;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What does rand() do in C? I don't use C++, just C.
Visual Studio 2012 tells me that its return type is int __cdecl
And it is part of stdlib.h
It does not take any parameters.
How can I set the range in which it generates (pseudo)random numbers?
Your answers are greatly appreciated
Now that there's an actual question: You can't. The range is fixed, and it's defined by the constant RAND_MAX if you need to know it (it's [0 .. RAND_MAX])
If you want a different range, you have to arrange that yourself, typically using the modulo operator % and optionally an offset ... for random numbers between 5 and 9 use
int foo = rand() % 5 + 5;
I use a helper function like this in the game i recently wrote:
int
randomNum(int min, int max)
{
static int seeded = 0;
if (!seeded)
{
seeded = 1;
srand((unsigned int)time(0));
}
return (rand() % (max-min+1)) + min;
}
Depends on the compiler. This wiki article contains a list of them: linear congruential generator
If you need to extend the range of rand(), call it multiple times and merge the outputs:
unsigned int r;
/* ... */
r = ((rand()>>4) & 0xff)<< 0;
r += ((rand()>>4) & 0xff)<< 8;
r += ((rand()>>4) & 0xff)<<16;
r += ((rand()>>4) & 0xff)<<24;
Example program to show how rand works by comparing the returned value. This works with Microsoft compiles (no mismatch).
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char* argv[])
{
unsigned int seed = 1;
unsigned int rand1, rand2;
unsigned int i;
for(i = 0; i < 20; i++){
seed = seed*214013 + 2531011;
rand1 = (seed >> 16) & 0x7fffu;
rand2 = rand();
if(rand1 != rand2)
printf("mismatch %d %d\n", rand1, rand2);
}
return(0);
}
Since only 15 bits of the seed are returned by this version of rand(), then RAND_MAX would be 32767 or hex 0x7fff. As noted in the wiki article, the period is 2^32, this means that seed will cycle through all 4,294,967,296 possible 32 bit values, never repeating until the 4,294,967,296 call to rand(), where seed will have cycled back to 1.
This question already has answers here:
How to generate a random integer number from within a range
(11 answers)
Closed 8 years ago.
I would like to generate random numbers between 1 and 25 with the method rand().
But I only know how to generate random numbers this way, which includes the number zero by default:
int r = rand() % 26 /* random int between 0 and 25 */
Anyone? Thank you.
Very simple
int r = 1 + rand() % 25 /* random int between 1 and 25 */
but you should use this
int r = (int)(1.0 + 25.0 * rand() / RAND_MAX)
as mentioned in the comments, the second is the more robust way to generate random numbers see this link
I'm trying to generate a simple random number in C with the raspberry pi. The code compiles fine but when running it the number is not random, its 384 each time.
Where am I going wrong?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf ("Random number generator\n") ;
int x = (rand() % 1000) + 1;
printf("%d\n", x);
return 0 ;
}
You need to seed the random number generator with some naturally random value like the current time. Something like:
srand (time(NULL));
Update: note that you will need to add an include for the time library if you use the example above:
include <time.h>
Use this function to generate random value. also make sure to add the library include <time.h>.
// Random value will be from 0 to #
int GenerateRandomInt (int MaxValue)
{
unsigned int iseed = (unsigned int)time(NULL); //Seed srand() using time() otherwise it will start from a default value of 1
srand (iseed);
int random_value = (int)((1.0 + MaxValue) * rand() / ( RAND_MAX + 1.0 ) ); //Scale rand()'s return value against RAND_MAX using doubles instead of a pure modulus to have a more distributed result.
return(random_value);
}
I want to generate 100 nodes with random x and y co ordinates. But i do not want to specify any range. Like rand(100) will generate numbers only between 1 to 100. But i want the numbers distributed over a large region and i want them to be random. How can i implement it using C?
i have tried:
int gen_rand_position(void)
{
int i,j,a[100],b[100];
for(i=0,j=0;i<100,j<100;i++,j++)
{
x=rand();
y=rand();
a[i]=x;
b[j]=y;
}
}
This not choosing randomly. Can i have more efficient random function?
You need to have a range otherwise what are you going to do with an infinite number?
With no arguments - rand() will return an integer between 0 and RAND_MAX ( normally 32765).
If you need a number larger than this you could combine two rand() numbers. There are complicated statistical arguments about the best way to combine random numbers so you don't change the randomness but I don't think you need to worry about that.
Edit: since RAND_MAX is (in this case) a 15bit number, to get a 30bit range multiply two rand() together, to get a 32bit range multiply again - it may wrap around but that doesn't change the randomness (significantly).
To obtain a random number distributed over the entire int range, combine the random bits from multiple calls to rand():
#include <stdlib.h>
int large_rand()
{
const int RAND_BITS = 15; /* covers stdc minimum for RAND_MAX */
const int INT_BITS = 8 * sizeof(int);
const int ITERS = (INT_BITS + RAND_BITS - 1) / RAND_BITS;
int i, result = 0;
for (i = 0; i < ITERS; i++) {
result <<= RAND_BITS;
result |= rand() & ~(~0U << RAND_BITS);
}
return result;
}
To get a random number in the desired range, use large_rand() % (MAX + 1), where MAX is the largest number you want to get.