# Explain the logic behind rand() to me..

• (2 Pages)
• 1
• 2

## 21 Replies - 1996 Views - Last Post: 15 October 2015 - 06:47 AM

### #1 Cansir

Reputation: 4
• Posts: 54
• Joined: 07-September 15

# Explain the logic behind rand() to me..

Posted 07 October 2015 - 03:16 AM

Why -in the bloody hell- would rand() need another line of code to seed it to to a specific position like time() to be even pseudorandom?

Java works by giving random numbers a min and max value in the arguments of the function.

Why.. why why why... why why... why the hell does this make any sense?

This post has been edited by Cansir: 07 October 2015 - 03:21 AM

Is This A Good Question/Topic? 0

## Replies To: Explain the logic behind rand() to me..

### #2 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 03:18 AM

The Random class in Java is doing the exact same thing behind the scenes.

You don't need to seed the random number generator, it will work fine without, but if you try it you will see that it produces the exact same set of random numbers every time.

Think of the seed as the starting place in the sequence of numbers produced by the generator. If you don't give it one then it will start from the beginning and look the same. By giving the time you tend to start from a different place every time in the sequence and so you get the pseudo-randomness you need.

This post has been edited by Ryano121: 07 October 2015 - 03:23 AM

### #3 Cansir

Reputation: 4
• Posts: 54
• Joined: 07-September 15

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 03:22 AM

Yes, though it is without needing to seed the random function.

Also, you can define the arguments, unlike C++.

What gives?

### #4 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 03:26 AM

C++ is lower level so it doesn't give you these nice helpers straight out of the box. They are not too difficult to define yourself though:

```// make sure to seed before calling!
int randomBetween( int lowerBound, int upperBound )
{
return rand() % (upperbound-lowerbound) + upperbound;
}
```

This post has been edited by Ryano121: 07 October 2015 - 03:26 AM

### #5 Cansir

Reputation: 4
• Posts: 54
• Joined: 07-September 15

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 03:43 AM

I have seen this example:

```srand(static_cast<unsigned int>(time(0)));
int preRand = rand();
int postRand = (preRand % 6) + 1;

```

It's all semantics at this point.
I'm just expressing my frustration.

### #6 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 03:50 AM

I'm not sure what you're frustrated about, that's what you've always gotten with C++. You get a lot of power and speed, but often have to do a little more work to get there. It's not the C++ style to give you a load of syntactic sugar and a ton of helper functions for every possible use case. It's possibly one of the good things about the language - it encourages you to learn what's going on behind all the fluff of other languages.

### #7 Cansir

Reputation: 4
• Posts: 54
• Joined: 07-September 15

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 03:56 AM

I like that explanation.
Quite a lot, actually.

I started off programming in Java and moved to C++ for portability and accessibility.

I also want to compile executables for Windows.

I'm just learning C++.
I am really enjoying it so far, and I seem to understand it quicker than Java.

I see that my opinion has changed about the language solely based on your comment.
I like the idea that this language brings you close(er) to the hardware.

Can you further explain the differences from Java to C++?

### #8 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 04:04 AM

Comparison

Quite a bit. The syntax looks quite similar in places but there are many differences. Google it and read up on it.

Biggest difference is ++ compiles directly to machine code and Java runs on top of a virtual machine (each have their advantages/disadvantages). You are correct in thinking C++ brings you closer to the hardware and pointers are a big thing in C++, Java doesn't have them (memory management is handled by the JVM).

They follow two quite different philosophies. Java has all the library methods to help you whereas C++ doesn't out of the box. This is again good and bad as you saw above you had to spend time writing that code for a simple random number. In Java you can get it straight out of the box, in C++ not so much but you get an idea of the inner workings.

### #9 Cansir

Reputation: 4
• Posts: 54
• Joined: 07-September 15

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 04:12 AM

Are you saying that with C++, you are required to find and download libraries you may need to code certain aspects as opposed to Java that has libraries that are preexisting?

### #10 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 04:16 AM

Pretty much. The standard C++ library was designed to be limited in scope (which again has it's advantages), but means it doesn't include a lot of stuff you may end up doing - networking, graphics, multithreadng, security etc etc...

C++ does have good libraries to do all that (Boost being a very popular example), but again you have to put it in yourself.

### #11 Cansir

Reputation: 4
• Posts: 54
• Joined: 07-September 15

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 04:19 AM

You are so copypasting the wiki page .

Ha. Anyway, thanks.

### #12 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 04:20 AM

That's because it's accurate and no need to retype everything

### #13 jon.kiparsky

• Screw Trump (before he screws you)

Reputation: 10625
• Posts: 18,185
• Joined: 19-March 11

## Re: Explain the logic behind rand() to me..

Posted 07 October 2015 - 06:53 AM

Cansir, on 07 October 2015 - 05:22 AM, said:

Yes, though it is without needing to seed the random function.

Well, if you look at the source code you'll find that, in the absence of an argument the generator is seeded with a suitable value.

Quote

I started off programming in Java and moved to C++ for portability and accessibility.

This is an interesting statement.

### #14 Ruben_

Reputation: 6
• Posts: 8
• Joined: 08-October 15

## Re: Explain the logic behind rand() to me..

Posted 08 October 2015 - 11:07 AM

Cansir, on 07 October 2015 - 03:16 AM, said:

Why -in the bloody hell- would rand() need another line of code to seed it to to a specific position like time() to be even pseudorandom?

Java works by giving random numbers a min and max value in the arguments of the function.

Why.. why why why... why why... why the hell does this make any sense?

First of all you need to understand that a computer CAN'T generate random numbers. It CAN only generate pseudo random numbers. The standard library has some built in random generators for different purposes (see: http://www.cplusplus...rence/random/). The seed is needed because it determines the sequence of numbers the random generator will spit out. A great example that leverages this feature of pseudo randomness is the game No Man Sky. Instead of having a fixed level layout it has a algorithm for creating the levels. And you probably already guessed it.. The seed determines how the level will look.

### #15 Cansir

Reputation: 4
• Posts: 54
• Joined: 07-September 15

## Re: Explain the logic behind rand() to me..

Posted 12 October 2015 - 02:02 AM

My argument was meant to state my disagreement with rand() not already being seeded to something, like time(0).

Though, I guess I can see how you may want pseudo-random, semi-predictable randomness..?

Dude, writing that I couldn't help but crank my head slowly to the side like wuuuut.?