| | |
Good webpage that explains how to use rand() and srand() correctly?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
>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.
New members chased away this month: 5
>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. New members chased away this month: 5
•
•
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
Views: 2024 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






