Random Integer

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 24042 Views - Last Post: 22 June 2011 - 02:10 AM Rate Topic: -----

#16 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: Random Integer

Posted 20 June 2011 - 03:42 PM

i agree with point as it applies to beginners. however as you become more experienced and understand how these abstractions could be made then it becomes less necessary to know the implementation details. e.g. abstraction, it's what OOP is about. take away and compartmentalize the nasty stuff and leave a nice clean OO shell of goodness.
Was This Post Helpful? 1
  • +
  • -

#17 hulla   User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 733
  • Joined: 05-March 11

Re: Random Integer

Posted 21 June 2011 - 08:38 AM

NickDMax... You are incredible and know exactly how I feel :smile2:
Anyway, sorry I'm such a pain in the butt but I tried:
int nConstant=srand(time(NULL));
with namespaces: stdlib.h, stdio.h, time.h, and math.h.

I also tried:

int nConstant=srand();//It thinks srand() is a function declaration


My compiler says: "error: void value not ignored as it ought to be."

This post has been edited by hulla: 21 June 2011 - 08:43 AM

Was This Post Helpful? 0
  • +
  • -

#18 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1594
  • View blog
  • Posts: 5,010
  • Joined: 20-March 10

Re: Random Integer

Posted 21 June 2011 - 09:01 AM

Its just
srand(time(NULL));

there is no return value


Best wishes Snoopy.
Was This Post Helpful? 0
  • +
  • -

#19 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: Random Integer

Posted 21 June 2011 - 12:19 PM

hulla: srand returns void, and not int. ;)

Also, what you call namespaces are headers. ;)
Was This Post Helpful? 0
  • +
  • -

#20 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Random Integer

Posted 21 June 2011 - 12:25 PM

srand()/rand() are found in cstdlib and time() in ctime:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
    //Step #1: Seed the random number generator with a value that will change each time
    //   the program is run. The current time works well:
    srand(time(0));
    // you really only need to do this once.
    
    cout << "100 random digits: " << endl;
    for(int i = 0; i < 100; ++i) {
        //method 1 for picking a random value between 0 and 9
        int random0to9 = rand() % 10;
        cout << random0to9 << ", ";
        if (i % 20 == 19) { cout << endl; }
    }
    
    // Using the remainder (%) can actually be bad because rand() tends to be
    // implemented as a LCG and may suffer from patterns in the lower bits.
    //  so it is actually better to use division:
    cout << "100 random 2 digit numbers: " << endl;
    for(int i = 0; i < 100; ++i) {
        //method 2 for picking a random value between 10 and 99
        int random10to99 = ((double)rand()/RAND_MAX) * (100 - 10) + 10;
        // the expression above generates a double value in the range [10, 100)
        //   but when converted to int any decimal values are truncated so 99.99 becomes 99
        //   so the range becomes [10, 99]
        cout << random10to99 << ", ";
        if (i % 10 == 9) { cout << endl; }
    }
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#21 hulla   User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 733
  • Joined: 05-March 11

Re: Random Integer

Posted 22 June 2011 - 02:04 AM

@Xupicor yeah sorry I had a thought transfer on my previous reply so I mixed up namespaces and headers :S
Was This Post Helpful? 0
  • +
  • -

#22 hulla   User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 733
  • Joined: 05-March 11

Re: Random Integer

Posted 22 June 2011 - 02:10 AM

Thanks Nick. I'll keep experimenting with the code till I learn how to get it to do what I want :stuart:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2