Is there a good webpage that explains how to use rand() and srand() correctly? Overall I need to learn how to generate a number between 0 and 3.

Dani AI

Generated

Quick, practical summary: rand and srand are the standard C library PRNG functions. To get one of the four values 0–3 you map the integer rand returns into that range (use the modulus idea with 4). Seed the generator once at program startup with srand (commonly seeded from the current time). Do not reseed inside a loop or before every draw — reseeding repeatedly is the usual cause of the repeating runs you saw.

Why the pattern happened: seeding with the current-time value (which has second resolution on most systems) and doing that inside a fast loop gives the same seed for many iterations, so the sequence repeats until the seed changes. That matches ’s symptom and is exactly what fixed by moving the seed out of the loop; ’s short “Yes” about calling srand only once is correct. Also, as noted, these functions are part of the C library so usage is the same in C and the C subset of C++.

Two practical caveats to keep in mind: (1) Modulo mapping can introduce a tiny bias whenever the PRNG’s full range isn’t an exact multiple of your target size — for 4 values and casual use that bias is negligible, but it exists. (2) Different platforms expose different RAND_MAX and PRNG implementations (older MSVC uses a much smaller RAND_MAX), and low-order bits from simple generators can be less random. If you need better uniformity or security, use rejection sampling (discard draws that would create bias) or switch to a stronger source (POSIX random, BSD arc4random, an OS entropy source like /dev/urandom, or a cryptographic library).

Checklist (quick fixes seen in this thread): seed once at startup, don’t reseed inside loops, accept modulo for casual 0–3 draws, and move to a better RNG or rejection sampling if you need stronger guarantees.

Recommended Answers

All 13 Replies

>This is C++.
That really changes nothing, rand and srand usage remains the same across the two languages. another one that I'd recommend for obvious reasons.

I do not understand the code. Thats why I wanted a page in C that tells me how to use rand()/srand() to generate a number between 0 and 3.

>I do not understand the code. Thats why I wanted a page in C that
>tells me how to use rand()/srand() to generate a number between 0 and 3.
Sorry, I'm not smart enough to dumb it down enough for you to understand. If you can't take int r = rand() % N; and turn it into int r = rand() % 4; with a two full tutorials backing it up, you're clearly out of my league.

>I do not understand the code. Thats why I wanted a page in C that
>tells me how to use rand()/srand() to generate a number between 0 and 3.
Sorry, I'm not smart enough to dumb it down enough for you to understand. If you can't take int r = rand() % N; and turn it into int r = rand() % 4; with a two full tutorials backing it up, you're clearly out of my league.

So what: N is now 4? But because you say so? And why 4/n?

If there is a page that explains the part of the funtion and the parameters, thats what Im looking for.

take int r = rand() % N; and turn it into int r = rand() % 4;

So what: N is now 4? But because you say so? And why 4/n?

You asked for 0-3 right? How many numbers are that? Holy ****! It's four!

If there is a page that explains the part of the funtion and the parameters, thats what Im looking for.

You really aren't the sharpest knife in the shed are you?

I'll give it a try :
You. Read. Tutorial.
Click this blue underlined

#include <stdlib.h> /* needed for rand() function */
#include <time.h> /*needed to call srand() (explained below)*/

int main(void)
{
	int r = 0;

	/* by calling this function, we seed the random number generator. If we don't seed it, we will get the
	same numbers every time we generate random numbers*/
	srand(time(0)); 

	/* rand() returns a number from 0 to RAND_MAX (you can output it to see)
	by using the % (modulus) operator on a number, the number you will get is from 0 to number-1 */
	r = rand() % 4;  /* so this returns a number from 0 to 3 */

	r = 1 + rand() % 4; /* 1 to 4  and so on*/

	return 0;
}

You asked for 0-3 right? How many numbers are that? Holy ****! It's four!


You really aren't the sharpest knife in the shed are you?

I'll give it a try :
You. Read. Tutorial.
Click this blue underlined

Again this is for C++, not C.

It is obvious that you are a complete asshole; Some are just starting out or simply need help to understand a certain area

#include <stdlib.h> /* needed for rand() function */
#include <time.h> /*needed to call srand() (explained below)*/

int main(void)
{
	int r = 0;

	/* by calling this function, we seed the random number generator. If we don't seed it, we will get the
	same numbers every time we generate random numbers*/
	srand(time(0)); 

	/* rand() returns a number from 0 to RAND_MAX (you can output it to see)
	by using the % (modulus) operator on a number, the number you will get is from 0 to number-1 */
	r = rand() % 4;  /* so this returns a number from 0 to 3 */

	r = 1 + rand() % 4; /* 1 to 4  and so on*/

	return 0;
}

Thank you for posting this code. It works but my only problem is that (in a do-while loop) it generates 0 about 10-15 times then 3 another 10-15 times then 3 another 10-15 times then 2 another 10-15 times then 1 another 10-15 times then 0 again and all over again....

I mean it really isnt random; I need to to generate it 0,3,2,1,3,0,2,0,1,2,3,1,0,2,2

then the next time i run it:

1,3,0,0,2,3,1,1,3,0,2,0,1,3,0

True random numbers :)


But thank you very much for posting working real working C code and not linking me to a C++ page.

I figured it out :) Had to run the seed outside of the do-while loop. Worked great.

One last question: Do I have to run srand(time(0)); only once in the entire program or each time I run a function called "int numbergenerator" which returns the random number?

Ill explain with code

running srand(time(0)); once:

void main()
{ 
int num;
srand(time(0));
int=numbergenerator;
}

int numbergenerator()
{
int num;
char c; //DO NOT PAY ATTENTION TO THIS. IT IS JUST SO THE DO-LOOP DOESN'T END AND GENERATES INFINITE TIMES AS A TEST
do
{
num=rand() % 4;
printf ("number %i",num);
getch(c);
while (c!="x"); //DO NOT PAY ATTENTION TO THIS. IT IS JUST SO THE DO-LOOP DOESN'T END AND GENERATES INFINITE AS A TEST
return num;
}

running srand(time(0)) each time

void main()
{ 
int num;
int=numbergenerator;
}

int numbergenerator()
{
int num;
char c; //DO NOT PAY ATTENTION TO THIS. IT IS JUST SO THE DO-LOOP DOESN'T END AND GENERATES INFINITE TIMES AS A TEST
srand(time(0))
do
{
num=rand() % 4;
printf ("number %i",num);
getch(c);
while (c!="x"); //DO NOT PAY ATTENTION TO THIS. IT IS JUST SO THE DO-LOOP DOESN'T END AND GENERATES INFINITE AS A TEST
return num;
}

Again this is for C++, not C.

I did not realize that you were unable to convert a command as cout to a command as printf .....

It is obvious that you are a complete asshole; Some are just starting out or simply need help to understand a certain area

I won't start a flamewar about the " ******* ".
If you're really trying to understand 'a certain area', you should really read the link I posted. I wasn't kidding, it's good info although there might be some C++ code involved.

Thank you for posting this code. It works but my only problem is that (in a do-while loop) it generates 0 about 10-15 times then 3 another 10-15 times then 3 another 10-15 times then 2 another 10-15 times then 1 another 10-15 times then 0 again and all over again....

So you have an error in your code/logic. Post your code because I don't have a magic crystal ball and I can't se your code from here. :icon_rolleyes:

One last question: Do I have to run srand(time(0)); only once in the entire program

Yes

[edit]
You might want to read this about using void main()

>I figured it out
It seems miracles do happen. Though through all your bitching and moaning about C++, you still don't seem to understand that srand and rand are used identically in both languages. They're a part of the C standard library and the C subset of C++.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.