Generating random numbers using VC6

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 30
Reputation: RisTar is an unknown quantity at this point 
Solved Threads: 0
RisTar RisTar is offline Offline
Light Poster

Generating random numbers using VC6

 
0
  #1
Apr 12th, 2007
Hi , im trying to create a simple code that generate random numbers
Im having two problem ,
1. For some reason randomize() function does not work on VisualC++6
2. I didnt really got the idea on how to set the range of the numbers that will be generated using the rand() function ,. i just saw it in some example .

Here is the code:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. int i;
  6. randomize();
  7. for (i=0;i<10;i++)
  8. printf ("Number %d: %d\n",i+1,rand()%10+1);
  9. return 0;
  10. }
Last edited by RisTar; Apr 12th, 2007 at 8:24 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 55
Reputation: addicted is an unknown quantity at this point 
Solved Threads: 0
addicted's Avatar
addicted addicted is offline Offline
Junior Poster in Training

Re: Generating random numbers using VC6

 
0
  #2
Apr 12th, 2007
hey,
i will like you to try this for C, i use VC6 but the program is C++ and i get my random numbers anytime i want....

first include this
  1. #include <cstdlib>
  2. using std::rand;

after that.. whenever you want to generate the random numbers,

  1.  
  2. rand()%4

wil generate one of 0, 1, 2, 3;

now if you want the number to be in the range 1 to 4, just use the code
  1.  
  2. 1 + rand()%4;

now you will get number in the range 1,2,3,4;

note 1 is the smallest and 4 is the largest;

if you want to generate the numbers 2, 4, 6, 8...
its by doing the same but adding 2 instead

  1.  
  2. 2 + rand()%4;
Last edited by addicted; Apr 12th, 2007 at 11:46 am. Reason: the code was for c++
00110101
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,133
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Generating random numbers using VC6

 
0
  #3
Apr 12th, 2007
Originally Posted by RisTar View Post
1. For some reason randomize() function does not work on VisualC++6
That's because this is C++, not Basic. You 'randomize' using srand(time()); once at the start of the program. Be sure to include time.h
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1784 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC