Good webpage that explains how to use rand() and srand() correctly?

Reply

Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Good webpage that explains how to use rand() and srand() correctly?

 
0
  #1
Nov 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #2
Nov 26th, 2008
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #3
Nov 26th, 2008
Originally Posted by devnar View Post
http://www.cprogramming.com/tutorial/random.html
This is C++.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #4
Nov 26th, 2008
>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.
Last edited by Narue; Nov 26th, 2008 at 3:44 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #5
Nov 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #6
Nov 26th, 2008
>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'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #7
Nov 26th, 2008
Originally Posted by Narue View Post
>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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,839
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #8
Nov 27th, 2008
Originally Posted by Narue View Post
take int r = rand() % N; and turn it into int r = rand() % 4;
Originally Posted by riahc3 View Post
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!
Originally Posted by riahc3 View Post
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #9
Nov 27th, 2008
  1. #include <stdlib.h> /* needed for rand() function */
  2. #include <time.h> /*needed to call srand() (explained below)*/
  3.  
  4. int main(void)
  5. {
  6. int r = 0;
  7.  
  8. /* by calling this function, we seed the random number generator. If we don't seed it, we will get the
  9. same numbers every time we generate random numbers*/
  10. srand(time(0));
  11.  
  12. /* rand() returns a number from 0 to RAND_MAX (you can output it to see)
  13. by using the % (modulus) operator on a number, the number you will get is from 0 to number-1 */
  14. r = rand() % 4; /* so this returns a number from 0 to 3 */
  15.  
  16. r = 1 + rand() % 4; /* 1 to 4 and so on*/
  17.  
  18. return 0;
  19. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #10
Dec 1st, 2008
Originally Posted by niek_e View Post
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 *******; Some are just starting out or simply need help to understand a certain area

Originally Posted by minas1 View Post
  1. #include <stdlib.h> /* needed for rand() function */
  2. #include <time.h> /*needed to call srand() (explained below)*/
  3.  
  4. int main(void)
  5. {
  6. int r = 0;
  7.  
  8. /* by calling this function, we seed the random number generator. If we don't seed it, we will get the
  9. same numbers every time we generate random numbers*/
  10. srand(time(0));
  11.  
  12. /* rand() returns a number from 0 to RAND_MAX (you can output it to see)
  13. by using the % (modulus) operator on a number, the number you will get is from 0 to number-1 */
  14. r = rand() % 4; /* so this returns a number from 0 to 3 */
  15.  
  16. r = 1 + rand() % 4; /* 1 to 4 and so on*/
  17.  
  18. return 0;
  19. }
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC