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.

>This is C++.
That really changes nothing, rand and srand usage remains the same across the two languages. Here's 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 word

#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 word

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++.

This article has been dead for over six months. Start a new discussion instead.