Help with a randomnumber generator

I'm working on an exercise that's in this PDF file I'm reading. What it wants me to do is...


Write a random-number generator that returns a random floatingpoint
number between 0.0 and 1.0. (Hint: Call rand, cast the result r to type double
by using static_cast<double>(r), and then divide by the highest value in the
int range, RAND_MAX.) Make sure you declare the function with the double
return type.


What my code looks like at the moment is...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

int randomNumber(double n);

int main()
{
	int r;

	srand(time(0)); // Gives random seed based off of time

	r = randomNumber(2.0); 
	r = static_cast<double>(r);
	cout << r << endl; // prints out the number

	system ("pause");
}

int randomNumber(double n)
{
	return rand() % RAND_MAX; // Needs to return a random number between 0.0 and 1.0
}



The program runs fine without any errors, but it'll only give me either 0 or 1 and not any double numbers as output. What am I not getting? lol. I'm unfamiliar with the "MAX_RAND" and "static_cast" usages. Did I use any of them right? Thanks in advance :)


EDIT: Also not sure if this matters, but the code I had started with from a previous exercise looked like this, in which I modified it to what it looks like at the top.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

int randomNumber(int n);

int main()
{
	int n, i;
	int r;

	srand(time(NULL)); // Gives random seed based off of time

	cout << "Enter number of dice to roll: "; 
	cin >> n; // Gets an input from the user.
	cout << endl;

	for (i = 1; i <= n; i++) // Loops for each number entered, specified by 'n'
	{
		r = randomNumber(6) + 1; // sets 'r' equal to the randomNumber function of 0-5 plus 1, to make it 1-6
		cout << r << endl; // prints out the number
	}

	system ("pause");
}

int randomNumber(int n)
{
	return rand() % n; // Returns a random number from 1-6
}
Last edited on
Make sure you declare the function with the double
return type.

You haven't done this, you changed the value the function is accepting to a double (which is no longer needed by the way as you are never using 'n') instead of the return type. You need to change it to
double randomNumber()
or else it will always implicitly convert the result to the an int (make sure you change the prototype above main too).

The next problem is you are dividing an integer by an integer which of course is going to give an integer result. You will want to cast one of these to a double to make sure it results in a double.
return rand() % static_cast<double>(RAND_MAX)

Finally you defined 'r' as an integer. You can't static cast a double into an integer variable as integers can never hold anything but whole numbers. You just want r to be a double to begin with.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

double RandomFloat() {
  srand(time(NULL));
  double rOutput = static_cast<double>(rand()) / RAND_MAX;
  return rOutput;
}
  

int main() {
  
  char getRand;
  cout<<"Would you like a random number between 0.0 and 1.0? (y/n)"<<endl;
  cin>>getRand;
  
  if(getRand == 'y') {
    cout<<RandomFloat()<<endl;
  }else{
    cout<<"Well then, this is awkward.."<<endl;
  }
  
  system("pause");
}


but I always get a number in the .74xxxx range.. odd

MAX_RAND is a defined value on a system that is the max random number possible to generate. So if we can generate from 0 to max, and then divide that number by the max, we're always guaranteed to only get a max of 1.0 (max/max)

As far as the cast goes, Im still wrapping my head around those kinds of casts, but I did notice a regular old cast such as double rOutput = (double)(rand()) / RAND_MAX; works just the same as double rOutput = static_cast<double>(rand()) / RAND_MAX; in this case.. perhaps someone can chime in there.
Last edited on
Okay so I modified my code and removed some things that weren't really needed (I don't know why I was using an 'r' variable) and combined the knowledge you guys gave me and received a working code...HOWEVER... I keep getting results around 0.72####. Would you happen to know why? It's not really random if all that really changes are the last 4 digits after the .72. Here's what my code now looks like.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

double randomNumber();

int main()
{
	srand (time(NULL));

	cout << randomNumber() << endl;

	system ("pause");
}

double randomNumber()
{
	return static_cast<double>(rand()) / RAND_MAX;
}
I mentioned the same thing in my first post.. the reason being, random is seeded by the clock time.

If you keep running the program over and over with only seconds between each runs, then the seed does not change very much, and thus the value of rand() doesnt change all that much. Though its "random" its really not, because its all dependent on the given seed value. Rand() will always return the same result given the same seed.

So run the program, wait a few mins, and then see what number it generates. I had written a loop to output multiple random numbers, and much to my dismay at first, they were all the same lol. I put in a 1 second sleep between each output and they were all very very close to each other.
Yeah I suppose that makes sense. Well it works none-the-less, so thank you :) I love programming but it can be a pain in the butt when trying to learn new things. Enjoying it so far though. I plan on going to college for it at some point, but I still want to learn what I can now.
Topic archived. No new replies allowed.