Like Tree1Likes
  • 1 Post By iMalc

Printing randomly generated numbers?

This is a discussion on Printing randomly generated numbers? within the C Programming forums, part of the General Programming Boards category; Hello, I have a program here that must generate 256 random numbers between 20 and -20 and write them to ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Printing randomly generated numbers?

    Hello, I have a program here that must generate 256 random numbers between 20 and -20 and write them to a file, numbers.txt. Then I must print the first 10 numbers generated. However with the code I have so far, all I get is the number 7, printed indefinitely on a loop. I'm thinking the j++ counter should be moved elsewhere, but I can't figure out where. Any help is appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    
    
    int rand( void ) ;
    
    
    int main( int argc, char *argv[] ) {
    
    
      int x, i = 0, j = 0 ;
      FILE *fin ;
      
      fin = fopen("numbers.txt", "a+") ;
    
    
      while ( i < 255 ) {
    
    
        x = rand() ;
        
        if (( x <= 20 ) && ( x >= -20 )){
    
    
        fprintf(fin, "%d ", x ) ;
    
    
        i++ ;
    
    
        j++ ;
    
    
          while( j <= 10 ){
    
    
    	printf("\n%d", x ) ;
          
          }
        }
      }
    
    
      fclose( fin ) ;  
    
    
      return 0 ;
    
    
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    594
    Look into seeding. There is a function known as srand() and you can include time.h to use time(NULL) inside the srand parameter.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Ah, I should've mentioned for this assignment I'm not allowed to use srand for this assignment. but thank you.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    594
    Are you suppose to make the random function? Where is your function definition for rand?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    If you're not allowed to use srand, then rand() is of no use to you either. You can't use one without the other.

    It probably means that you have to implement another random number generator by yourself, or what I would recommend is getting the XORShift algorithm code from Wikipedia.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by iMalc View Post
    If you're not allowed to use srand, then rand() is of no use to you either. You can't use one without the other.

    It probably means that you have to implement another random number generator by yourself, or what I would recommend is getting the XORShift algorithm code from Wikipedia.
    Another interpretation is that he's not supposed to use srand so that the output it easier to mark. Either that or he's mistaken.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Code:
        j++ ;
    
    
          while( j <= 10 ){
    
    
    	printf("\n%d", x ) ;
          
          }
    Can you see INSIDE the loop where j is being modified?

    If you can't see where j is modified inside the loop, how do you expect j<=10 to become false at some point and break the loop.

    When you understand this, can you think where a good place to put j++ would be?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by Salem View Post
    Code:
        j++ ;
    
    
          while( j <= 10 ){
    
    
    	printf("\n%d", x ) ;
          
          }
    Can you see INSIDE the loop where j is being modified?

    If you can't see where j is modified inside the loop, how do you expect j<=10 to become false at some point and break the loop.

    When you understand this, can you think where a good place to put j++ would be?
    I suspect that the inner while loop is supposed to be just an if-statement rather than a loop. In that case it would be possible for the increment to stay where it is. Of course in that case j is also redundant as i could be used instead.
    Salem likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vBulletin vandals and the wisdom of randomly generated passwords
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-08-2008, 04:27 PM
  2. Replies: 1
    Last Post: 03-17-2005, 07:53 AM
  3. sorting a randomly generated dynamic array
    By Led Zeppelin in forum C Programming
    Replies: 4
    Last Post: 05-03-2002, 09:10 AM
  4. unique randomly generated numbers
    By grobe59 in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2001, 07:26 PM
  5. arranging randomly generated numbers in ascending order
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 06:14 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21