Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

C++ Random # Generation

Name: Anonymous 2006-11-22 1:36

So, how can I randomly generate numbers into a program like this?

double triangleArea(double t)
{
        double base;
        double height;
        double answerT;
        cout << "Enter the Base of the Triangle: ";
        cin >> base;
        cout << "Enter the Height of the Triangle: ";
        cin >> height;
        answerT = .5 * base * height;
        cout << "The Area of the Triangle is: " << answerT << "." << endl;
        return answerT;
}

This is inside a function, because I have selection of multiple functions inside main. I want the program to randomly generate numbers into the function itself so the user doesn't have to.

I tried to change the cin's to base = rand(); and height = rand();...this doesnt work...in fact, even if i take the cout's out of the function, it still cout's those strings...WTF?

Name: Anonymous 2006-11-22 6:19

#include <stdlib.h>
i = rand();

Name: Anonymous 2006-11-22 8:21

>>2
That doesn't help too much. :/

Name: Anonymous 2006-11-22 9:10

Name: Anonymous 2006-11-22 11:13

in fact, even if i take the cout's out of the function, it still cout's those strings...WTF?
Did you fail to recompile after editing the code?

Name: Anonymous 2006-11-22 11:53

>>5
Yeah, I recompiled. Which is why I just gave a big wtf. :(

Name: Anonymous 2006-11-22 12:35

Ok, so if you didn't like >>4, then

www.opera.com

Name: Anonymous 2006-11-22 13:19

>>6

PEBKAC

Name: Anonymous 2006-11-23 0:34

#include <stdlib.h>
int randomNumber = rand() % 11;

This will set randomNumber between 0-10.

To set a non zero minimal number you want.

int randomNumber = (rand() % 6) + 5;

This will set randomNumber between 0-5 and then add 5, making it 5-10.
This will be good enough for a majority of your programming, later you might nees something better, in time youll learn.

Name: Anonymous 2006-11-23 4:47

>>9

Looking at his program, I think he wants a random double. In which case it would be best to cast rand() to a double, divide by RAND_MAX (to get a random number between 0 and 1) and then linear transform to the desired range using formula y = ax + b.

Name: Anonymous 2006-11-24 7:09

Using an IDE or compiling on the command line? It's possible, though stupid, for an IDE to recompile your program to one place, and run another, older version elsewhere.

Name: Anonymous 2006-11-24 9:53

Don't use rand() without setting the seed with srand(int), else the results will be predictable (and actually, always the same from one execution to the other)

Name: Anonymous 2006-11-24 10:36 (sage)

The random() function uses a non-linear additive feedback random number generator employing a default table of size 31 long integers to return successive pseudo-random numbers in the range from 0 to (2**31)-1.  The period of this random number generator is very large, approximately 16*((2**31)-1).

The random() and srandom() functions have (almost) the same calling sequence and initialization properties as the rand(3) and srand(3) functions.  The difference is that rand(3) produces a much less random sequence -- in fact, the low dozen bits generated by rand go through a cyclic pattern.  All the bits generated by random() are usable.  For example, `random()&01' will produce a random binary value.

Name: Anonymous 2006-11-24 14:37

>>13

Careful though, they're non-standard and as such won't appear on all systems. All BSD-derived systems should have them though.

Name: Anonymous 2006-11-24 17:30

All BSD-derived systems should have them though
So Microsoft Windows and OS X, but not Linux?

Name: Anonymous 2006-11-24 18:53

>>15
He said that all BSD-derived systems should have the functions, not that only BSD-derived systems have them.

Name: Anonymous 2006-11-24 19:06

>>15

I don't know where you got the idea that Windows is derived from BSD. Those calls are only available in their optional Unix subsystem addon.

Yes, OS X does have them. As does Linux.

One example of a Unix that doesn't have them is Solaris 2.4 (2.5 and above do have them though.)

Name: Anonymous 2006-11-24 20:58

What the fuck, people. Still calling the old, unreliable stdlib random functions, some of which aren't even portable.

Why didn't you fucking read what I posted in >>4 and Googled for a C or C++ implementation of the Mersenne Twister which is what you should be using if you're minimally serious with pseudo-random numbers.

Name: Anonymous 2006-11-25 9:28

>>18

Well, it depends how important the randomness is to you. If you just need something kind of random (e.g. to use with the original code he posted) rand and srand will be fine.

Name: Anonymous 2006-11-25 9:55

>>17
Windows is derived from BSD with "copy/paste license."

Name: Anonymous 2006-11-25 12:36

>>20
Windows (unfortunately) only took small parts of it, such as sockets.

Name: Anonymous 2006-11-25 17:05

>>20

No

Name: Anonymous 2006-11-25 18:50

>>19
Mersenne Twister is only pseudorandom too, but it's more random (whatever that means lulz) and it's faster.

Name: Anonymous 2006-11-26 2:45

ISAAC > MT

Name: Anonymous 2006-11-26 8:01

>>21
how do you know? have you seen the code?

Name: Anonymous 2006-11-26 9:33

>>25

I don't know if he has, but I've got a copy of a large chunk of the NT4 and 2000 source code. I think later I'll search it for BSD and see what comes up.

Name: Anonymous 2006-11-26 12:57

>>25
It's just that other than sockets and a few small things, it would be impossible to copypasta BSD code into such a different system.

Name: Anonymous 2006-11-26 14:05

>>26
yeah right... torrent or stfu

Name: Anonymous 2006-11-26 14:08

>>26

Easiest way is just to look at all the makefiles. Most of them are straight copy pastas.

Name: Anonymous 2006-11-26 19:14

>>29
Really?

Name: Anonymous 2006-11-26 20:11

Name: Anonymous 2006-11-26 20:38

LUPUS

Name: Anonymous 2006-11-27 19:00

>>31

Huh, he wasn't lying after all

Name: Anonymous 2006-11-28 11:05

>>33

Not remember the new stories when it leaked? Was a big story about a year ago. I thought everyone had the code.

Name: !/MrpyThONg 2008-01-09 13:05

lol?

Name: Anonymous 2009-01-14 13:47

VALID PERL CODE

Name: Anonymous 2009-02-25 8:00


Column 4 unexpected end   of input expected.

Name: Anonymous 2009-03-06 8:24

The fact that you   can map to   what SIDs I.

Name: Trollbot9000 2009-07-01 8:42

The scenegraph and myriad?

Name: Anonymous 2009-07-12 6:44

>>39
You You trousers trousers   Optimised   5/10 to will I keep this ,....OOo.   ,....OOo.  GPL3 Person their X Foobar more One night time back far(and basically so pipes commands

Name: Sgt.Kabu둖ፓkiman詁ఄ 2012-05-28 20:56

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Name: Anonymous 2013-07-27 16:09

check 'em

Don't change these.
Name: Email:
Entire Thread Thread List