# c++ random numbers

## Recommended Posts

So Im stuck on problably a relativley simple part. I know about using the random seed generator, but our teacher hasnt gone over it much.

this is a function i have to make here's what it's asking

"getrand - a double function that returns a random number from 0 to 1"

so I have to make a double function.....ok no prob, it would just need one paramter right

like

double getrand(double random);

then in the actual function

double getrand(double random)

{

}

but I dont know what would go in the function, it needs to get a random decimal between 0 and 1.

later on I have to match it to a number that i physically put in like .567 or something, then it's going to see if the number I put in is lower than the number that was generated (it's like mimicing a computer virus program or something(

any clue? anybody? on what would go in the function?, and it has to keep generating a random number X amount of times.? so it'd have to be different each time and not the same "random" number

here's the website

http://www.cs.uky.edu/~keen/115/programs/5pgm.html

but Im mainly just confused on the getrand function

thx!

##### Share on other sites

You don't need to pass anything into the funtion. Try something like this:

```double getrand()
{
srand((unsigned)time(0));
double x = (rand() % 10 + 1)
return x;
}```

That will return a number from 1 to 10.

The idea is to seed the random number generator (RNG) with the current time, so that if you call it again a few seconds later, it's not the same. When you hear people talking about "truely random numbers", nine times out of ten they're just seeding the generator with time.

##### Share on other sites

but when I try to do that with a decimal it doesnt work? it says something about double blah blah. like if I wanted it to be a random decimal number between 0 and 1

##### Share on other sites
but when I try to do that with a decimal it doesnt work? it says something about double blah blah. like if I wanted it to be a random decimal number between 0 and 1

Couldn't you just divide the answer by 10 before you return it. It mathmatically makes sense, but it has been a while since I have done much in c++.

How many decimal places are you looking at having, btw?

##### Share on other sites

```#include <iostream>
#include <time.h>

using namespace std;

double getRand(); //Function prototype

int main(void) {
...

}

double getRand() {
srand((unsigned)time(NULL)); //Seed rand func
double theRand = (rand() % 10 + 1) / 10;
...
return(theRand); }```

That'll give you a good start. However, it's not the full program, nor will it do as you want. It's your job to modify the code and figure it out ^_^.

##### Share on other sites

for some reason it keeps saying that the "time" identifier is not defined, im using visual basic 2005/ and how would I call that function anyways

EDIT: whoops nm

edit2: i keep getting 0's though for the random number tho when I called it to display the cout of the function? any idea why?

but taking the % of something then dividing it by 10 tho? wouldnt that always give u a 0

reedit for 5000th time: ok i got it, i had to make it 10 + 1.0 to give me a decimal :-D

Edited by CoolMaster

##### Share on other sites

Good to hear you figured that out ^_^. It's the little things in programming that make all the difference.

##### Share on other sites
reedit for 5000th time: ok i got it, i had to make it 10 + 1.0 to give me a decimal :-D

Always remember what type you are dealing with. 1 is an integer, 1.0 is a floating point number.