# Trying to figure this out

Page 1 of 1

## 3 Replies - 390 Views - Last Post: 09 August 2010 - 01:02 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=185248&amp;s=4d0578a44c02c002e3cb2d6651ae6054&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jim1963

Reputation: 0
• Posts: 4
• Joined: 11-September 09

# Trying to figure this out

Posted 09 August 2010 - 07:45 AM

What possible values can the following code snippet generate? Assume that rand(n) returns an integer between 0 and n-1, inclusive.

int random_value = floor(((2.5*rand(3)) + 2)^2)
Is This A Good Question/Topic? 0

## Replies To: Trying to figure this out

### #2 PennyBoki

• system("revolution");

Reputation: 53
• Posts: 2,335
• Joined: 11-December 06

## Re: Trying to figure this out

Posted 09 August 2010 - 07:46 AM

Is it a test question?

You could check if you run the code.

### #3 eker676

• Software Engineer

Reputation: 378
• Posts: 1,833
• Joined: 18-April 09

## Re: Trying to figure this out

Posted 09 August 2010 - 11:31 AM

Just break it down.

int random_value = floor(((2.5*rand(3)) + 2)^2)

Possible values for rand(3) can be 0, 1, and 2

Then do basic math.

((2.5*0) + 2) ^ 2 = ...

((2.5*1) + 2) ^ 2 = ... Then round down to the nearest integer.

((2.5*2) + 2) ^ 2 = ...

If you need further help, well, you shouldn't.

This post has been edited by eker676: 09 August 2010 - 11:32 AM

### #4 jim1963

Reputation: 0
• Posts: 4
• Joined: 11-September 09

## Re: Trying to figure this out

Posted 09 August 2010 - 01:02 PM

eker676, on 09 August 2010 - 10:31 AM, said:

Just break it down.

int random_value = floor(((2.5*rand(3)) + 2)^2)

Possible values for rand(3) can be 0, 1, and 2

Then do basic math.

((2.5*0) + 2) ^ 2 = ...

((2.5*1) + 2) ^ 2 = ... Then round down to the nearest integer.

((2.5*2) + 2) ^ 2 = ...

If you need further help, well, you shouldn't.

Thank you. I understand now.