| | |
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.
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.
>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
>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.
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
•
•
•
•
>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 takeint r = rand() % N;and turn it intoint r = rand() % 4;with a two full tutorials backing it up, you're clearly out of my league.
If there is a page that explains the part of the funtion and the parameters, thats what Im looking for.
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
•
•
•
•
If there is a page that explains the part of the funtion and the parameters, thats what Im looking for.
I'll give it a try :
You. Read. Tutorial.
Click this blue underlined word
C Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 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
It is obvious that you are a complete *******; Some are just starting out or simply need help to understand a certain area
•
•
•
•
C Syntax (Toggle Plain Text)
#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; }
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.
![]() |
Other Threads in the C Forum
- Previous Thread: deleting special characters from string of numbers
- Next Thread: Arrays and Pointers
| Thread Tools | Search this Thread |
* api array asterisks binarysearch calculate changingto char character cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database feet fgets file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram homework i/o infiniteloop input interest intmain() iso kernel keyboard kilometer km linked linkedlist linux locate logical_drives looping loopinsideloop. lowest meter microsoft mqqueue number oddnumber odf open opendocumentformat openwebfoundation owf pdf performance posix power probleminc process programming pyramidusingturboccodes radix read recv recvblocked research reversing scanf scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall threads turboc unix urboc user variable wab whythiscodecausesegmentationfault win32api windowsapi






