RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 6517 | Replies: 17 | Solved
Reply
Join Date: Aug 2004
Location: London
Posts: 235
Reputation: Thinka is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 5
Sponsor
Thinka's Avatar
Thinka Thinka is offline Offline
Posting Whiz in Training

Re: Using the rand() function in C

  #11  
Apr 29th, 2007
OK, I'm being silly, this is the correct version of the code that compiles and runs.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int i;
  5.  
  6. void generate_ascending_number(int n)
  7. {
  8. int available = RAND_MAX;
  9. int required = n;
  10.  
  11. for (i=0; i<available; ++i)
  12. {
  13. if ( (rand() % (available-i)) < required)
  14. {
  15. printf("%d\t", i);
  16. }
  17.  
  18. }
  19.  
  20. }
  21.  
  22. int main()
  23. {
  24. generate_ascending_number(20);
  25. return 0;
  26. }

Thanks very much vijayan! Now when it runs I get 176 numbers printed out in sequence... How can I get more control over how many it prints out, because I'll be wanting to do up to 10,000, and possibly more? Is it by playing around with line 12?
Never approach a computer with the words "I'll just do this quickly..."
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,087
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Using the rand() function in C

  #12  
Apr 29th, 2007
the parameter passed to the function is the number of ascending random integers to be printed.
modify line 24 to
generate_ascending_number(1024);
to print 1024 numbers.
Reply With Quote  
Join Date: Aug 2004
Location: London
Posts: 235
Reputation: Thinka is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 5
Sponsor
Thinka's Avatar
Thinka Thinka is offline Offline
Posting Whiz in Training

Re: Using the rand() function in C

  #13  
Apr 29th, 2007
Hmmm. I've put 1024 in there, and it sure prints out a lot of numbers, which might well be 1024 numbers. But when I put in 10, it prints out 98 numbers, and when I put in 5, it prints out 50 numbers. I've tried this more than once now... any ideas why this is happening please?
Never approach a computer with the words "I'll just do this quickly..."
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,087
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Using the rand() function in C

  #14  
Apr 29th, 2007
you forgot to do
--required

in the for loop (line 14-16)
note: the loop could be made more efficient by adding if(required==0) break ;
Last edited by vijayan121 : Apr 29th, 2007 at 1:33 pm.
Reply With Quote  
Join Date: Aug 2004
Location: London
Posts: 235
Reputation: Thinka is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 5
Sponsor
Thinka's Avatar
Thinka Thinka is offline Offline
Posting Whiz in Training

Re: Using the rand() function in C

  #15  
Apr 29th, 2007
Yea you're right, now it works. Thanks so much, you've been amazing. But may I please ask a couple more things: "--required" decrements that variable so that the number entered into the function is the number of numbers printed out right?
And how can I adjust the range pls?I tried putting a variable with a smaller number in the place of 'available' in line 13 & it compiled, but when i ran the program I got an 'Arithmetic Exception'. Sorry to keep hassling like this.
Never approach a computer with the words "I'll just do this quickly..."
Reply With Quote  
Join Date: Aug 2004
Location: London
Posts: 235
Reputation: Thinka is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 5
Sponsor
Thinka's Avatar
Thinka Thinka is offline Offline
Posting Whiz in Training

Re: Using the rand() function in C

  #16  
Apr 29th, 2007
Again, apologies, so embarassing, I solved the 2nd qstn, I have to use the same value in lines 11 & 13 if I'm going to use something different from 'available'.

Thanks v much for all your help vijayan, it's very much appreciated!
Last edited by Thinka : Apr 29th, 2007 at 1:56 pm.
Never approach a computer with the words "I'll just do this quickly..."
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,087
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Using the rand() function in C

  #17  
Apr 29th, 2007
see thread at
http://www.daniweb.com/techtalkforum...tml#post345699
this fills an array, instead of printing out the random number; but the idea is the same
Reply With Quote  
Join Date: Aug 2004
Location: London
Posts: 235
Reputation: Thinka is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 5
Sponsor
Thinka's Avatar
Thinka Thinka is offline Offline
Posting Whiz in Training

Re: Using the rand() function in C

  #18  
Apr 29th, 2007
Yes, that IS a useful thread, thanks again!
Never approach a computer with the words "I'll just do this quickly..."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 4:20 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC