| | |
random number
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 146
Reputation:
Solved Threads: 0
i am trying to generate random numbers in a loop
i wrote this function
but every time i use it in a loop i get the same number
i wrote this function
C Syntax (Toggle Plain Text)
int getRand ( int a , int b ) { static int x = 0 ; if ( x == 0 ) { srand ( time( NULL ) ) ; x++ ; } return a+rand()%(b-a+1); }
but every time i use it in a loop i get the same number
Last edited by emilio; May 1st, 2008 at 12:53 pm.
•
•
Join Date: Nov 2007
Posts: 146
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
for(i=0 ; i<5 ; i++) { chid = fork(); if (chid == 0) { n = getRand(0,9); printf("child number %d goes to sleep for %d seconds\n",i+1,n); sleep(n); kill(getpid(),SIGKILL); } }
![]() |
Similar Threads
- Random number generator's (C++)
- Need to know how to create a database that will generate a random number (Database Design)
- Random number generation (C)
- Help with random number gen (C++)
Other Threads in the C Forum
- Previous Thread: Help with reading more than one digit
- Next Thread: Game of Mastermind
Views: 628 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
#include .net ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mysql number opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windowsapi






