Results 1 to 4 of 4

Thread: srand

  1. #1
    t420h
    Guest

    srand

    does anyon know how to use the srand function? i have to generate a rndom number for a math prblem btwn 1..10 but i want them to be different everytime. i understand srand does this but i cat get it to work.


    heres what i have

    cout<<"generating random numbers between 1 and 10.....\n";


    temp1 = tempA+double(rand())*((tempB-tempA)/RAND_MAX);
    temp2 = tempA+double(rand())*((tempB-tempA)/RAND_MAX);

    cout<<"\nThe first number is "<<temp1;
    cout<<"\nThe second number is "<<temp2<<"\n";
    cout<<"\nCan you figure out "<<temp1<<oper<<temp2<<" ?\n";
    cin>>UserAns;
    system("CLS");
    return 0;

  2. #2
    jim mcnamara
    Guest
    [code]

    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main(void){
    int i, stime;
    long ltime;
    ltime = time(NULL);
    stime = (unsigned) ltime/2;
    srand(stime);
    for (i=0;i<10;i++) printf("%d ",(int))10* (float)(rand()/RAND_MAX)));

    printf("\n");
    return 0;
    }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    stime;
    long ltime;
    ltime = time(NULL);
    stime = (unsigned) ltime/2;
    srand(stime);


    Why?
    Why not simply
    srand(time(NULL));
    ?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    jim mcnamara
    Guest
    long and int are not the same for the compiler that was written for. Avoids overflow.

    In VC++ you would not need to do that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width