srand() in .Net

This is a discussion on srand() in .Net within the C++ Programming forums, part of the General Programming Boards category; Hello, I am new to .Net and more of an old school programmer. I for the life of me canít ...

  1. #1
    Sad Programmer
    Guest

    srand() in .Net

    Hello,

    I am new to .Net and more of an old school programmer. I for the life of me canít make srand() work in .Net. this is the line of code that has always worked for me in the past:

    srand(time(NULL));

    The Error I get is:

    error C2365: 'srand' : redefinition; previous definition was a 'function'

    And

    error C2501: 'srand' : missing storage-class or type specifiers

    Now that damn intellisense or however you spell it tells me that srand() will return an int when I hover my mouse over the function call. So I thought that the computer knew what was going on so I tried to catch that int in a variable only to be told:

    error C2440: 'initializing' : cannot convert from 'void' to 'int'

    So after searching many web sites and books the only way I can find to seed the rand() is with srand(). Have I been left in the dark? Has the call to the system clock changed? What is going on with my lousy srand() call?

    I include time.h and I have not redefined srand() or rand() at all. I am new to namespace std so maybe I have messed something up in there.

    Thanks.

    P.S. Someone suggested ::srand(time(NULL)); but that yields the same errors.

  2. #2
    Sad Programmer
    Guest

    Sure here is the code.

    //make our numbers super random
    #include <time.h>

    srand(time(NULL));


    //returns a random integer between x and y
    int RandInt(int x,int y)
    {
    return rand()%(y-x+1)+x;
    }

    //returns a random float between zero and 1
    double RandPercent()
    {
    return (rand())/(RAND_MAX+1.0);
    }

    //returns a random bool
    bool RandBool()
    {
    if (RandInt(0,1))
    {
    return (true);
    }
    else
    {
    return (false);
    }
    }

    //returns a random float in the range -1 < n < 1
    double RandomClamped()
    {
    return (RandPercent() - RandPercent());
    }


    this is the code from a rand.h header I made.

  3. #3
    Sad Programmer
    Guest
    Makes sense.

    Thanks.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>this is the code from a rand.h header I made.
    But code like that shouldn't be in a .h file....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    2

    why?

    I posted eairlier under the handle Sad Programmer.

    Why not? What would you suggest to do? I have need varying random numbers for functions in different source files. Are you trying to suggest that I should have just made a .cpp file and my choice in calling it .h was mistaken? Is there a better way to get many different random number functions into other source files, without including some sort of header file? I am not the best at C++ nor am I very up to date. I am attempting to do a C++ project, and the last time I coded anything in C++ was three years ago.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    .... something like this multi-source file example
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    2

    thanks

    Ah ha! It makes sense again. I have been making my classes that way, but I forgot why I did it. Thanks for the refresher. I am sure I have forgotten many other things that will come back to bite me. I guess that is what I get for programming in prolog the last few years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. migrate from .Net 2.0 to .Net 3.0
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 07-25-2007, 04:07 AM
  2. Some .NET Distribution Stats
    By nickname_changed in forum C# Programming
    Replies: 2
    Last Post: 05-14-2005, 03:41 AM
  3. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  4. .net
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 02-15-2002, 12:15 AM
  5. Visual J#
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 02:41 PM

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