If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > how to to generate random number within 0 to 10 in c language

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Jan 2004
Location: kuala lumpur
Posts: 18
how to to generate random number within 0 to 10 in c language

how to to generate random number within 0 to 10 in c language
can i use rand() to make it? how to use?
can i set the range for rand()?
if i just use rand() it make generate 2000++ possibilityvalue
Please help me.
thank you
Reply With Quote
  #2 (permalink)  
Old
Registered User
 
Join Date: Jan 2006
Posts: 2
I copied this example from the help pages, was quicker than writting my own...i've editted it for 10 (was 100 in the example)

Obviously replace printf with which ever use you need the numbers for


#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(void)
{
int i;

randomize();
printf("One random number from 0 to 9\n\n");
printf("%d\n", rand() % 10);
return 0;
}


=======
for 2000+ use


#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(void)
{
int i;

randomize();
printf("2000 random numbers from 0 to 9\n\n");
for(i=0; i<2000; i++)
printf("%d\n", rand() % 10);
return 0;
}



=======
for numbers between 10 -> 20 use ===> (rand() % 10) + 10;

Last edited by AlkieDropout; 01-13-06 at 03:16. Reason: typo correction
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On