rand() & srand() not working as i want them to...O-o

This is a discussion on rand() & srand() not working as i want them to...O-o within the C++ Programming forums, part of the General Programming Boards category; Hey everyone it's meh the noob again... (you "sigh" and roll your eyes) I have abit of a problem with ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Australia
    Posts
    5

    Question rand() & srand() not working as i want them to...O-o

    Hey everyone it's meh the noob again... (you "sigh" and roll your eyes)

    I have abit of a problem with the rand() and srand()
    functions

    i looked through some forum searches for a solution to my first problem..

    Problem (1):
    When i use something like
    Code:
    int number_of_noobs_in_this_thread = rand()%3;
    it keeps coming up with the same number
    now i found out why (partly) it's because of something about the seed and that each hardware randoms up the same number (or something like that)

    so i look to the forums and found out about srand()

    i was like "Finally something that actually works"
    until this : 107|error: 'time' was not declared in this scope|

    i had used the srand(time(NULL));
    to see if that works and without it it goes

    note: declared here

    but on line 353 ??? wtf???

    so now im stuck and no i have only used srand() once and my
    #include <iostream>
    #include <cstdlib>

    are all i have in it ???

    Please help ?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    You also need
    #include <ctime>
    to prototype the time function.
    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.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Australia
    Posts
    5
    Quote Originally Posted by Salem View Post
    You also need
    #include <ctime>
    to prototype the time function.
    ...

    ...

    ...

    oh ... well it didnt say that anywhere?

    but thanks anyway

    hopefully i start to get better at this

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    Quote Originally Posted by Max Winzar View Post
    ...
    oh ... well it didnt say that anywhere?
    Virtually every bit of documentation I have seen about the time() function says it is in <time.h> (in C) or <ctime> (in C++).

    Granted, it is sometimes left out of examples about the srand() and rand() functions because - unfortunately - some implementations incorrectly declare it in <stdlib.h> (C) or <cstdlib> (C++).

    Basic lesson though: all functions and types in the standard library are declared in a standard header. So, if you are using a standard function as intended (right number and type of arguments, etc) and the compiler chokes, then you forgotten to #include the relevant header. From there it is not hard to find what the right header is by looking at relevant documentation (even google helps if you give it "C++ time", with a link to the ctime header).
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    You will have to remember that everytime you use a standard library function, you must look up in what header it resides in and add it to your list of includes.
    Also, if your compiler supports it, prefer using nullptr instead of NULL (MSVC 10+, GCC 4.6+ supports it).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    Quote Originally Posted by Elysia View Post
    You will have to remember that everytime you use a standard library function, you must look up in what header it resides in and add it to your list of includes.
    Also, if your compiler supports it, prefer using nullptr instead of NULL (MSVC 10+, GCC 4.6+ supports it).
    C++11 to the rescue. dan dandan daaaaaaan!
    How to ask smart questions

    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesWritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Srand and Rand not working correctly?
    By lebronlin in forum C Programming
    Replies: 13
    Last Post: 04-06-2011, 01:18 AM
  2. srand and rand
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2008, 07:09 AM
  3. Rand and srand
    By JFonseka in forum C Programming
    Replies: 5
    Last Post: 02-26-2008, 09:36 PM
  4. srand() and rand()
    By Mr.Sellars in forum C++ Programming
    Replies: 3
    Last Post: 08-12-2007, 03:19 PM
  5. srand() or rand()???
    By bajanstar in forum C Programming
    Replies: 4
    Last Post: 03-04-2005, 11:58 AM

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