Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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++)
            {
                RandomizeAnswers();
            }
 
public void RandomizeAnswers()
{
    int x = rn.Next(7);
     // Then printing...
    
}
Posted 6-Feb-13 7:58am
Edited 8-Feb-13 4:31am
v3
Comments
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: bad
good
Please Sign up or sign in to vote.

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[^]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

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[^]
  Permalink  
Comments
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: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v2
Comments
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: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Try using this
 
int x = rn.Next() % 7;
 
Maybe it helps you.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

This might solve your problem:
 
Random rand = new Random((int)DateTime.Now.Ticks);
int numIterations = 0;
numIterations=  rand.Next(0, 7);
Response.Write(numIterations.ToString());
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

Radnom r = new Radnom();
int temp;
for (int i =0; i<=7; i++)
{
temp = r.next(0,7);
Console.WriteLine(temp);
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  

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

  Print Answers RSS
0 Maciej Los 225
1 OriginalGriff 140
2 Sergey Alexandrovich Kryukov 137
3 _Amy 115
4 manoj kumar choubey 115
0 OriginalGriff 7,320
1 Sergey Alexandrovich Kryukov 6,043
2 Maciej Los 3,749
3 Peter Leow 3,408
4 DamithSL 2,555


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100