-5

I have just started learning c++, so my question may seen extremely downscaled.

Can anyone please explain srand function properly?
ALSO, is there a srand replacement (except for rand) which is more simpler and doesn't using printf func?

I am currently using this code:

#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <iostream.h>
#include <conio.h>
#include <string>
#include <math>
#include <ctype>

int main ()
{
float a;
start:  
  a=srand (time(NULL));
cout<<a;
getch();
goto start;
}

which provides me with the following error:
1. Srand not an allowed function.

Any help would be appreciated. :)

Harshal Gajjar
  • 265
  • 1
  • 4
  • 12
  • 3
    Is that really your error? =/ – Joseph Mansfield Jul 01 '14 at 15:23
  • 1
    it looks like you went here: http://www.cplusplus.com/reference/cstdlib/srand/. You should then note that srand doesn't return anything – Ben Jul 01 '14 at 15:26
  • 1) Sort out your headers (there is no iostream.h anymore, only `iostream`, also conio.h looks fishy.) 2) get rid of that goto – Erbureth Jul 01 '14 at 15:26
  • 6
    Wherever you're learning C++ from looks to be teaching you some pretty bad practices! – Joseph Mansfield Jul 01 '14 at 15:27
  • Replace your `goto` with a `while (1)` loop or similar. Include `iostream` instead of `iostream.h`. You can't use `cout` the way you are, either. Either add `using namespace std;` or use `std::cout`. I suggest you grab a good book on C++. – aglasser Jul 01 '14 at 15:30
  • Actually I am using turbo 3.0 for the basic start in c++, and there `using namespace std;` and other stuff don't work. – Harshal Gajjar Jul 01 '14 at 15:47
  • And can you suggest a good book? :) – Harshal Gajjar Jul 01 '14 at 15:47
  • 1
    @HarshalGajjar Yes, we can: [**The Definitive C++ Book Guide and List**](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) :) – πάντα ῥεῖ Jul 01 '14 at 16:07
  • 2
    @HarshalGajjar _'Actually I am using turbo 3.0'_ Please get a more modern compiler for learning also. Turbo C++ is pretty ancient and was released before there was even a C++ standard established. It often forces you to write code, that simply is considered wrong nowadays. I well know, that you might need to use turbo c++ at school, but at least for your private studies get on with a modern compiler. – πάντα ῥεῖ Jul 01 '14 at 16:27

2 Answers2

3

srand() does not return a value. I doesn't do what you apparently think it does. It seeds the random number generator, but does not generate a random number.

You are looking for rand(), although that's not the best way to generate random numbers in C++11.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
1

srand seeds rand. I believe you want to try something like

int main ()
{
    float a;
    srand(time(NULL));
    while(true)
    {  
        a=rand();
        cout<<a;
        getch();
    }
}

Although rand returns an int, not a float, if you are looking to generate a random float it will take extra work.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
Eumcoz
  • 2,388
  • 1
  • 21
  • 44