Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Serac

macrumors newbie
Original poster
Jan 26, 2006
4
0
Taking an example from the program above, I’d like to clarify a question I’ve been having.

c++
Code:
#include <iostream> 
#include <ctime> 
#include <cstdlib> 
using namespace std; 

int main() 
{ 
srand((unsigned)time(0)); 
int rn; 
int lowest=1, highest=100; 
int range=(highest-lowest)+1; 
for(int index=0; index<20; index++){ 
rn=lowest+int(range*rand()/(RAND_MAX + 1.0)); 
} 
if (rn<50)
	{
        cout<<”A random number is under 50”;
        cout<<"("<<rn<<")";
        }


So, with this program, it should supply a random number always different when run, right? Only 1, so I’d have to copy lines 8-14 again if I wanted a different random number?

Btw, I’m trying to get a “random” number from 1-100.

I’d appreciate any help. :)
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
srandomdev();
int rand = (random() * 10000) % 100;

This is just off the top of my head, but iirc it works fine. random() is better than rand().
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1

srand(time(0));
int a = rand() % 100 + 1;

This gives you a random number between 1 and 100 using srand and rand. Or you can probably use srandomdev as Catfish suggests, but I'm not familiar with that one.

Catfish, why do you multiply by 10000? If random() returns a long or int, that would make your expression evaluate to 0 every time, wouldn't it?
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
gekko513 said:

srand(time(0));
int a = rand() % 100 + 1;

This gives you a random number between 1 and 100 using srand and rand. Or you can probably use srandomdev as Catfish suggests, but I'm not familiar with that one.

Catfish, why do you multiply by 10000? If random() returns a long or int, that would make your expression evaluate to 0 every time, wouldn't it?

hmm, somehow I'd remembered random() as returning a double between 0 and 1. Like I said, that was just off the top of my head.
 

Thomas Harte

macrumors 6502
Nov 30, 2005
400
4
gekko513 said:
int a = rand() % 100 + 1;
The general advice when using rand() is that if you can't use all the bits, you should use the higher ones in preference to the lower ones. That's because many "random number" algorithms make a good job of covering the range described by RAND_MAX but a bad job of making every bit truly random, as a result of trying to maximise randomness without spending relatively large processing costs and the choice of random number algorithm is up to whoever implements rand().

If you could manage it without overflow, ((rand() * range) / RAND_MAX) would therefore always be better than rand()%range. On the Microsoft platform RAND_MAX is only (2^15)-1 or (2^16)-1 so you sometimes see that where people haven't really thought about what they're doing. On OS X RAND_MAX is 0x7fffffff - i.e. the highest positive number that can be stored in an signed int. So you do have to think about range.

If you're not that bothered about the degree of randomness then %range will probably do. If you can afford the costs, casting to floats and doing a multiply/divide op will supply the best variation. If you can write specifically for your range you can do something like:

((rand() >> 8)*range) / (RAND_MAX >> 8)

(for ranges up to 256, obviously). So for a random number in the inclusive range 0 to 100 you might try:

((rand() >> 7)*101) / (RAND_MAX >> 7)
 

Fender2112

macrumors 65816
Aug 11, 2002
1,141
402
Charlotte, NC
Here's a little program the asks you to guess a number between 1 and 100. I think the concepts you want can be derived from this simple program.

#include <iostream>

using namespace std;

int main()

{
system("clear");

srandom(time(NULL)); // seed random number generator from internal clock
int fave;
fave = (random() % 100) + 1; // use random seed to generate number
int n;

//cout << fave << endl;

cout << "Guess a number from 1 to 100: ";
do
{
cin >> n;
if (n < fave)
cout << "Too low. Guess again: ";
else if (n > fave)
cout << "Too high. Guess again: ";
else
cout << fave << "\a\a\a ***** You must be psycic! ****" << endl;
} while (n != fave);

return 0;
}
 

Fender2112

macrumors 65816
Aug 11, 2002
1,141
402
Charlotte, NC
Serac said:
So, with this program, it should supply a random number always different when run, right? Only 1, so I’d have to copy lines 8-14 again if I wanted a different random number?

Btw, I’m trying to get a “random” number from 1-100.

I’d appreciate any help. :)

You can write a function and call it each time you need a random number

void GetRandomNumber() // function prototype

void GetRandomNumber() // function definition
{
srandom(time(NULL));​
return (random() % 100)+1;​
}

x = GetRandomNumber(); // call to function
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
Fender2112 said:
You can write a function and call it each time you need a random number

void GetRandomNumber() // function prototype

void GetRandomNumber() // function definition
{
srandom(time(NULL));​
return (random() % 100)+1;​
}

x = GetRandomNumber(); // call to function

Since you are reseeding the random number generator each time you invoke this method then the generated numbers can be predicted. (Also, the function needs to be declared as returning an integer.)

What you really want to do in this case is:

Code:
int getRandomNumber();

short randomSeeded = 0;

main() {
  printf("   %d\n", getRandomNumber());
  printf("   %d\n", getRandomNumber());
  printf("   %d\n", getRandomNumber());
}

int getRandomNumber() {
    if(randomSeeded == 0) {
       srandom(time(NULL));
       randomSeeded = 1;
    }

    return (random()%100) + 1;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.