Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.80/5 (5 votes)
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 ??
C#
           Random rn=new Random();

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

public void RandomizeAnswers()
{
    int x = rn.Next(7);
     // Then printing...
    
}
Posted
Updated 8-Feb-13 4:31am
v3
Comments
Irbaz Haider Hashmi 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 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 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 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 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

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[^]
 
Share this answer
 
v2
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[^]
 
Share this answer
 
Comments
bbirajdar 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....
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
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 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 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 6-Feb-13 18:27pm    
Anyway, code is not adequately shown...
—SA
for (int i = 0; i < 50; i++)
{
RandomizeAnswers();
}
 
public void RandomizeAnswers()
{
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
 
Share this answer
 
Try using this

int x = rn.Next() % 7;


Maybe it helps you.
 
Share this answer
 
This might solve your problem:

C#
Random rand = new Random((int)DateTime.Now.Ticks);
int numIterations = 0;
numIterations=  rand.Next(0, 7);
Response.Write(numIterations.ToString());
 
Share this answer
 
Radnom r = new Radnom();
int temp;
for (int i =0; i<=7; i++)
{
temp = r.next(0,7);
Console.WriteLine(temp);
}
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900