Rate this:
See more:
I am using `Random()` method to generator random numbers from 0 to 7. When I put it in a loop I'm getting this kind of result:
`2222266666666111111`
How can I fix it ??
```           Random rn=new Random();

for (int i = 0; i < 50; i++)
{
}

{
int x = rn.Next(7);
// Then printing...

}```
Posted 6-Feb-13 7:58am
Edited 8-Feb-13 4:31am
v3
Irbaz Haider Hashmi at 6-Feb-13 18:04pm

You have a small range that's why it seems that the numbers might not be random. Try it with 1000 and you will see that it has random values.
Sergey Alexandrovich Kryukov at 6-Feb-13 18:21pm

Agree. The question is not properly formulated, code not properly shown (please see my comment below).
—SA
Sergey Alexandrovich Kryukov at 6-Feb-13 18:19pm

Why public? why not passing rn as a parameter? Are you sure you use the same instance of rn in all calculations of rn.Next?
And finally, what's wrong with your output? What "this kind of results" means? If it's not always exactly the same, it's random...

So, main the question is: how RandomizeAnswers() accesses rn.
—SA
missak boyajian at 7-Feb-13 7:25am

Hey I passed rn.Next(7) as a parameter It worked. Why this way giving this kind of result? Like 1111222110000033333...
Sergey Alexandrovich Kryukov at 7-Feb-13 8:49am

We can continue discussing this problem only if you create absolutely complete but short code sample. It should be in one file, console application, be very short, but it should compile right away. Simple, isn't it? Otherwise, we are just wasting time.

There can be many ways to screw up things, but in this case it's very hard to imaging what could be wrong. Why taking guesses?

—SA
jibesh at 6-Feb-13 18:28pm

when I tried the same the O/P is like '4546140625242560151206.....' both with rn.Next(7) and rn.Next(0,7)

how did you defined your random variable? can you post the actual code so that we can find the real problem.
LokoLuke at 8-Feb-13 11:46am

First you should specify a seed in your Random() constructor. As siny13 said you should use (int)DateTime.Now.Ticks to ensure that each new instance wont return the same sequences.

Also you are using the same max value of 7 so that limits how random you can have it.
Also your loop is iterating 50 times so your going to have a lot of duplication.

Rate this:

## Solution 1

It could be that you aren't seeding it properly. Can't really tell because you didn't show your code, but when random() doesn't work as expected, it's usually the case.
Check out the MSDN documentation: System.Random[^]
v2
Rate this:

## Solution 5

You are right. But if you want to generate a 50 digit number, you should use a bigger range..

See this description from MSDN

"The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random. By default, the parameterless constructor of the Random class uses the system clock to generate its seed value, while its parameterized constructor can take an Int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers. The following example illustrates that two Random objects that are instantiated in close succession generate an identical series of random numbers."

http://msdn.microsoft.com/en-us/library/system.random.aspx[^]
aspnet_regiis -i at 7-Feb-13 13:51pm

I was never able to understand why people take pleasure in down voting without specifying reason...May be I am wrong in trying to help others solve their difficulties....
Rate this:

## Solution 2

You should set the min, max range Random.Next[^]

```Random rnd  = new Random();
//inside your loop use it like
int newNumber = rnd.Next(0,7); // gets the random number between 0-7```
v2
Sergey Alexandrovich Kryukov at 6-Feb-13 18:17pm

No, Next with 1 parameter works correctly as well. OP already does it, so it cannot solve the problem. Please see my comments to the question.
—SA
jibesh at 6-Feb-13 18:24pm

SA OP add those code after I put the solution. in both the case it should work. since OP tried the other I smell the random variable definition might be inside the scope or he made some mistake in other part of the code.
Sergey Alexandrovich Kryukov at 6-Feb-13 18:27pm

Anyway, code is not adequately shown...
—SA
Rate this:

## Solution 4

```for (int i = 0; i < 50; i++)
{
}

{
int x = rn.Next(0,7);      //it will display values between 0,7; confirm
// Then printing...
Console.WriteLine(x);
} ```

Dont hesitate in asking any Question

Thanx
Rate this:

## Solution 6

Try using this

`int x = rn.Next() % 7;`

Maybe it helps you.
Rate this:

## Solution 7

```Random rand = new Random((int)DateTime.Now.Ticks);
int numIterations = 0;
numIterations=  rand.Next(0, 7);
Response.Write(numIterations.ToString());```
Rate this:

## Solution 8

int temp;
for (int i =0; i<=7; i++)
{
temp = r.next(0,7);
Console.WriteLine(temp);
}
Rate this:

## Solution 9

First you should specify a seed in your Random() constructor. As siny13 said you should use (int)DateTime.Now.Ticks to insure that each new instance wont return the same sequences.

Also you are using the same max value of 7 so that limits how random you can have it.
Also your loop is iterating 50 times so your going to have a lot of duplication.