Generating random numbers...

This is a discussion on Generating random numbers... within the C Programming forums, part of the General Programming Boards category; hey there... i need to create a random numbers generator.. i solve the code already.. but.. how am i going ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38

    Generating random numbers...

    hey there...
    i need to create a random numbers generator..
    i solve the code already..
    but.. how am i going to have my program keep on running..
    for example:
    "PRESS ANY KEY TO CONTINUE"
    (then.. the program run from the beginning..)

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    40
    You should learn how to use the looping structure in C. You can just google it out.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    i'd try to insert while repetition structure...
    but, it cant be stop.. and "PRESS ANY KEY TO CONTINUE" isnt functioning..
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
    int i;
    time_t t;
    srand((unsigned) time(&t));
    while (i != 0) {
    printf("Printing 5 random numbers:\n");
    for(i=0; i< 5;i++)
    printf("%d\n",rand()%100);
    printf("Press any key to continue..\n");
    }
    }

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Firstly you have never assigned i a value so you shouldnt be checking its value.

    also, if you want your loop to go on forever, a better way would be -

    while(1) or while(true)

    and "PRESS ANY KEY TO CONTINUE" isnt functioning..
    Yes, The computer doesn't wait for a key press just because you printed it on the screen.

    Cprogramming.com FAQ > How do I get my program to wait for a keypress?

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    got it!! thanks spidey!!

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Smile So, is this one correct spidey?

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

    int main(void)
    {
    int i;
    time_t t;
    srand((unsigned) time(&t));
    while (1) {
    printf("Printing 5 random numbers:\n");
    for(i=0; i< 5;i++)
    printf("%d\n",rand()%100);
    printf("Press any key to continue..\n");
    }
    }

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    #include<stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main(void)
    {
    int i;
    time_t t;
    srand((unsigned) time(&t));
    while (1) {
    printf("Printing 5 random numbers:\n");
    for(i=0; i< 5;i++)
    printf("%d\n",rand()%100);
    printf("Press any key to continue..\n");
    }
    }
    Is it correct?

  8. #8
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    That is correct, though not as correct as it could be with [code][/code] tags. In any event, you should also be aware that you could just as easily do

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
      int i;
      srand((unsigned) time(0)); /* the pointer is an optional parameter */
      while (1) {
        printf("Printing 5 random numbers:\n");
        for(i=0; i< 5;i++)
          printf("%d\n",rand()%100);
        printf("Press any key to continue..\n");
        getchar();
      }
    }
    so that one actually has to press a key to continue.
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 12:29 PM
  4. Random Number Generating
    By K.n.i.g.h.t in forum C Programming
    Replies: 9
    Last Post: 01-30-2005, 01:16 PM
  5. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM

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