Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 28, 2006, 12:30 PM   #1
Serac
macrumors newbie
 
Join Date: Jan 2006
Random numbers for c++

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.

Last edited by Serac; Jan 28, 2006 at 01:00 PM.
Serac is offline   0 Reply With Quote
Old Jan 28, 2006, 03:55 PM   #2
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
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().
Catfish_Man is offline   0 Reply With Quote
Old Jan 28, 2006, 05:39 PM   #3
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003

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?
gekko513 is offline   0 Reply With Quote
Old Jan 29, 2006, 03:41 PM   #4
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Quote:
Originally Posted by gekko513

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.
Catfish_Man is offline   0 Reply With Quote
Old Jan 29, 2006, 04:53 PM   #5
Thomas Harte
macrumors 6502
 
Join Date: Nov 2005
Quote:
Originally Posted by gekko513
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)
Thomas Harte is offline   0 Reply With Quote
Old Jan 29, 2006, 09:00 PM   #6
Fender2112
macrumors 6502a
 
Fender2112's Avatar
 
Join Date: Aug 2002
Location: Charlotte, NC
Send a message via AIM to Fender2112
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;
}
__________________
Really? Really, really!
Fender2112 is offline   0 Reply With Quote
Old Jan 29, 2006, 09:14 PM   #7
Fender2112
macrumors 6502a
 
Fender2112's Avatar
 
Join Date: Aug 2002
Location: Charlotte, NC
Send a message via AIM to Fender2112
Quote:
Originally Posted by Serac
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
__________________
Really? Really, really!
Fender2112 is offline   0 Reply With Quote
Old Jan 29, 2006, 09:14 PM   #8
Fender2112
macrumors 6502a
 
Fender2112's Avatar
 
Join Date: Aug 2002
Location: Charlotte, NC
Send a message via AIM to Fender2112
delete double post
__________________
Really? Really, really!
Fender2112 is offline   0 Reply With Quote
Old Jan 30, 2006, 12:31 AM   #9
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
Quote:
Originally Posted by Fender2112
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;
}
mrichmon is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
My keychain access shows random numbers and letters for my passwords, please help :) xcstryder OS X Mavericks (10.9) 1 Apr 15, 2014 12:33 AM
Numbers vs Excel when mixing text & numbers TheBlackLabEdge Mac Applications and Mac App Store 5 Mar 6, 2014 08:29 PM
Advanced Search starts with lots of "random" numbers as keywords simsaladimbamba Site and Forum Feedback 6 Feb 18, 2014 10:32 PM
How to generate quality random numbers in bulk? Qaanol Mac Programming 7 Jul 21, 2013 01:46 PM
MBP 2010 random/not so random crashes? Kobaine MacBook Pro 0 Jan 3, 2013 04:10 PM

Forum Jump

All times are GMT -5. The time now is 02:51 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC