943,712 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4748
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 26th, 2008
0

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

Expand Post »
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.
Reputation Points: 49
Solved Threads: 1
Posting Whiz in Training
riahc3 is offline Offline
298 posts
since May 2008
Nov 26th, 2008
0

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

Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Nov 26th, 2008
0

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

Reputation Points: 49
Solved Threads: 1
Posting Whiz in Training
riahc3 is offline Offline
298 posts
since May 2008
Nov 26th, 2008
1

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 26th, 2008
0

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

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.
Reputation Points: 49
Solved Threads: 1
Posting Whiz in Training
riahc3 is offline Offline
298 posts
since May 2008
Nov 26th, 2008
1

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 26th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by Narue ...
>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.
Reputation Points: 49
Solved Threads: 1
Posting Whiz in Training
riahc3 is offline Offline
298 posts
since May 2008
Nov 27th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by Narue ...
take int r = rand() % N; and turn it into int r = rand() % 4;
Click to Expand / Collapse  Quote originally posted by riahc3 ...
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!
Click to Expand / Collapse  Quote originally posted by riahc3 ...
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
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 27th, 2008
0

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

  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. }
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Dec 1st, 2008
0

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

Click to Expand / Collapse  Quote originally posted by niek_e ...
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

Click to Expand / Collapse  Quote originally posted by minas1 ...
  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.
Reputation Points: 49
Solved Threads: 1
Posting Whiz in Training
riahc3 is offline Offline
298 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: deleting special characters from string of numbers
Next Thread in C Forum Timeline: Arrays and Pointers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC