Not Yet Answered # Math tutor

Discussion Starter buddyrocks73 Duoas 1,025 ithelp 757 invisal 381 Nick Evan 4,005 Narue 5,707 iamthwee 1,547 iamthwee 1,547 Duoas 1,025 Roebuc Narue 5,707 Roebuc Narue 5,707

0

So far I have:

```
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
unsigned seed;
cout <<"Enter a seed value: ";
cin >>seed;
srand (seed); //Call srand to set a "seed" before
//any random numbers can be generated
cout <<rand() <<endl;
cout <<rand() <<endl;
cout <<rand() <<endl;
return 0;
}
```

0

rand() returns a value between 0 and RAND_MAX.

You need to apply some math to bound that value. In C++, the remainder operator is **%**. `int value = (rand() % 499) +1;`

0

rand() returns a value between 0 and RAND_MAX.

You need to apply some math to bound that value. In C++, the remainder operator is

%.`int value = (rand() % 499) +1;`

% operator removes randomness quite a bit.

1

rand() returns a value between 0 and RAND_MAX.

You need to apply some math to bound that value. In C++, the remainder operator is

%.`int value = (rand() % 499) +1;`

I believe the largest value that your code can generate is 499 :D

0

You're very right: rand()

so for number between 1-500 use: `value= (rand() % 500) +1;`

All credits for Invisal!

% operator removes randomness quite a bit.

Oh?

Niek

0

>% operator removes randomness quite a bit.

A problem which is statistically insignificant unless you have very specific distribution and period needs (in which case you'd use a much better random number generator) or the range is very small (such as `rand() % 2`

). For simple programs that just need something "random enough", using remainder to fix the range is fine. More details about what's happening and why (as well as how to fix problems properly) can be found here.

0

A problem which is statistically insignificant unless you have very specific distribution and period needs (in which case you'd use a much better random number generator) or the range is very small (such as rand() % 2). For simple programs that just need something "random enough", using remainder to fix the range is fine. More details about what's happening and why (as well as how to fix problems properly) can be found here.

Wow, if you take out the waffle that Julienne dude would have a great, informative website.

0

The waffle adds to its charm.

I find the comic sans font around the circular logo endearing too.

0

I believe the largest value that your code can generate is 499 :D

Yoinks! Half asleep math again!

Sorry :$

0

Ok...I am just a student and a noob one at that so fill free to correct me and raze me into the ground so I learn, but last night in my reading and doing an assignment I worked with rand() for the first time. To quote:

% operator removes randomness quite a bit.

My book did say something about this and I believe its answer for fixing that was this: (using the example from before)

`int value = ((rand() + time(0)) % 499) +1;`

Now I don't have my book right infront of me now as I am suppose to be working, so it is possible my syntax is wrong but hopefully that is enough to give you, the pro's ;), a good enough idea of what I am trying to talk about. Something about the `time(0)`

helping make it more random. Ok....let the razing begin!

**Edit:** I corrected a ":(" problem as I did not mean to have a sad face, I am quite happy actually, thanks for asking.

0

>int value = ((rand() + time(0)) % 499) +1;

Interesting, but not a good idea. First, using a time_t value (the type that time returns) in basically any arithmetic operation besides comparison to (time_t)-1 isn't guaranteed to be meaningful. Second, unless time_t is unsigned (and it may not be), you're risking integer overflow with `(rand() + time(0))`

. Finally, this won't fix the distribution problem of using remainder to adjust the range of rand. In fact, it's more likely to make it worse. ;)

>Something about the time(0) helping make it more random.

I'd guess that the book meant using time(0) as the argument to srand, which should be called once when the program starts to seed your random number generator:

```
#include <cstdlib>
#include <ctime>
#include <iostream>
int main()
{
std::srand ( std::time ( 0 ) );
for ( int i = 0; i < 10; i++ )
std::cout<< std::rand() % 10 <<'\n';
}
```

All that does is changes the sequence between runs of the program. rand is seeded to 1 by default, so if you don't call srand, you'll keep getting the same sequence if you run the program multiple times. That's a different issue from using the remainder operator to fix the range that rand returns, which is an issue of all values in the range no longer being equally probable.

0

ok.....so I was typing out this long @$$ post on telling you what I was doing for the assignment and how it worked and then I remembered.

I believed I had to use the time(0) to lock the number not make it more random. I was learning how to do while loops for the first time and use a bool to end the loop. I think the (rand() + time(0)) made the loop hold on to the same number until the loop ended. The user was suppose to guess a number between 0 and 100, and have 5 tries to guess the number. So, in that respect, last night running the program multiple times, rand() never once picked the same number. So the only reason I can think of that the % makes it not as random is because you are restricting the pool the computer can pull from. A number btwn 1 & 10 will never be as random as a number btwn 1 & 1000. Right?

0

>A number btwn 1 & 10 will never be as random as a number btwn 1 & 1000. Right?

Close. rand is designed to return a number between 0 and RAND_MAX, where each number in that range is equally probable. That means every time you call rand, you have a 1 in RAND_MAX chance of getting every number between 0 and RAND_MAX. If you restrict the range with the remainder operator, chances are very good that you'll break that distribution such that some numbers in the new range are more likely to be returned than others. So the sequence isn't as "random" as it would be if you didn't restrict the range.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

I don’t want at this stage work on a big separate project as I've already got plenty ...

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`