randomize in C

This is a discussion on randomize in C within the C Programming forums, part of the General Programming Boards category; I'm trying to generate a program that generates numbers between 1 and 1000 and then prints out if it is ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Cool randomize in C

    I'm trying to generate a program that generates numbers between 1 and 1000 and then prints out if it is prime or not. I tried using randomize but it didn't work. I get an error message that says "too few arguments to function (srand)".
    here is the code I have so far.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
            int prime(int n)
    {
            int i;
            int n;
             randomize();
            i = srand();
            for(i = 2; i < 1000; i++){
                    if ( n%i == 0 && i != n)
                    return 0;
    
    
            }
            return 1;
    }
    int main()
    {
            int n:  
            n = prime(n);
            printf("%d\n", n);
    return 0;
    }
    Last edited by torquemada; 03-10-2011 at 05:02 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you have randomize (a non-standard function), then you probably don't need srand. Look up the srand function to see what it expects for its arguments. You could also just look at the FAQ section for the one on random numbers.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    int n:  
            n = prime(n);
    n is not initialized with any value.... So, it should give an compile time error..

    Second...
    Code:
    Include time.h
    then
    srand(time(NULL))
    then 
    int x=rand()%whatever_Range_You_Want;

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    n is not initialized with any value.... So, it should give an compile time error..
    That depends on your compiler and or compile options
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Whatever compiler you are using.....
    If n got no value, what the whole program will do?
    Just look into the whole program and see there are multiple operations that are being performed with n while n has no value....

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    Whatever compiler you are using.....
    If n got no value, what the whole program will do?
    Just look into the whole program and see there are multiple operations that are being performed with n while n has no value....
    Obviously it should be initialised, i am saying that not every compiler will output an error if a variable is not initialised.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Nevermind... Peace out... :-)

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Mr.777 View Post
    n is not initialized with any value.... So, it should give an compile time error..
    Warning, yes. Error? Not unless you're specifying it to halt compiling on every warning. Even then, it's not really an error.

    You are right about a compile time error, but for the wrong reason.
    Code:
            int n:  
            n = prime(n);
    THAT is why you'll get a compile time error. Not because n is used uninitialized, but because that's a : and not a ;. Also:
    Code:
    int prime(int n)
    {
            int i;
            int n;
    There should at least be a warning about a redeclaration of the variable named n here. If not, turn up your compiler warnings.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomize array blackjack c program
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-01-2005, 02:05 PM
  2. don't want to randomize two alike numbers
    By randomizer in forum C++ Programming
    Replies: 8
    Last Post: 05-26-2005, 07:42 PM
  3. randomize hex digits
    By wazilian in forum C Programming
    Replies: 3
    Last Post: 12-14-2002, 02:20 AM
  4. Problem with Visual C++ ( randomize function )!
    By marCplusplus in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2001, 12:01 PM
  5. Randomize Number Function
    By beyonddc in forum C Programming
    Replies: 3
    Last Post: 12-10-2001, 03:31 AM

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