-3

I want to create random numbers multiple times. In the snippet below, i try to generate 100 numbers between 0 and 5 from the same random variable. Still , i am getting the same numbers consecutively . I just want that my next number should be different from prev one generated.

            Random random = new Random();

            for(int i=0;i<100;i++)
            {
                int num   = random.Next(5);
                Console.WriteLine(num);
            }

Picture of how the numbers were showing up .

Attached the picture of how the numbers were showing up !

kchahat20
  • 97
  • 1
  • 11
  • 1
    Maybe something like this: https://stackoverflow.com/a/1011408/13687491 – nanu_nana Jun 30 '20 at 05:08
  • It's not clear what you are asking. Are you (a) looking for a method to produce random numbers without repetitions, or (b) concerned that the presence of repetitions means that your method isn't producing a truly random sequence? – Jarmo Jun 30 '20 at 05:12
  • @Jarmo As far as I can tell, OP simply doesn't want a sequence like 4444. I assume 404142, etc. is acceptable. – ProgrammingLlama Jun 30 '20 at 05:14
  • 1
    Go get a board game like Monopoly. Pull out a single six-sided die. Roll it 100 times. Are you surprised to see repetitions? The die you are working with only has 5 sides – Flydog57 Jun 30 '20 at 05:16
  • 2
    [That's the problem with randomness: You can never be sure](https://dilbert.com/strip/2001-10-25) – Sir Rufo Jun 30 '20 at 06:41
  • This is quite normal when you habe only such a limited numer of choices. – TaW Jun 30 '20 at 07:48
  • For what it's worth, but rejecting repeated responses (as suggested in the answers and in the comments to to answers) you are making your sequence less random – Flydog57 Jun 30 '20 at 12:05

2 Answers2

2

In a truly random sequence, you will naturally get repetitions. If you want to artificially suppress repetition, try something like:

        Random random = new Random();
        int last = -1;
        for(int i=0;i<100;i++)
        {
            int num;
            do {
              num = random.Next(5);
            } while(num == last)
            last = num;
            Console.WriteLine(num);
        }
see sharper
  • 11,505
  • 8
  • 46
  • 65
  • I actually want to do that inside a parallel for loop like : ` Parallel.For(0,100,()=>{ //code to generate next random });` . How can i do that ? – kchahat20 Jun 30 '20 at 05:32
  • 3
    I have to ask, why on earth? – see sharper Jun 30 '20 at 05:32
  • 3
    Parallel means you can't meaningfully talk about consecutive numbers until *after* the numbers have been generated. Which you can do of course by picking out repetitions after the fact and filling in the resulting blanks, but why are you so keen to avoid repetitions? – see sharper Jun 30 '20 at 05:35
  • Actually , i am trying to send data from multiple files to a single file in azure data lake service parallelly, and i have to do this via buffers, but have this restriction that i can only use 5 buffers at max. Now i cannot increment variables or keep counters as this is prallell , so thought of generating some random number btw 0 to 5 , and use it as buffer counter – kchahat20 Jun 30 '20 at 05:37
  • Probably not the right solution. You should be asking about *that* problem, not the question you asked, because random numbers aren't the answer. – see sharper Jun 30 '20 at 05:46
1

You'll need to track the previous number and compare it to the current one:

int? lastNum = null; // variable for tracking the last generated number
// I've chosen a nullable int since it will cover any random number range
for(int i=0;i<100;i++)
{
    int num = 0;
    do
    {
        num  = random.Next(5);
    }
    while (lastNum.HasValue && num == lastNum.Value); // if the lastNum was set and is the same value, loop and generate a new number
    lastNum = num; // store the last value for the next iteration.

    Console.WriteLine(num);
}

Of course you could still end up with a sequence like 40142434, etc. but that's life.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • I actually want to do that inside a parallel for loop like : ` Parallel.For(0,100,()=>{ //code to generate next random });` . How can i do that ? – kchahat20 Jun 30 '20 at 05:32
  • 1
    That doesn't make a whole lot of sense. Why do you need a parallel loop to generate 100 numbers? – ProgrammingLlama Jun 30 '20 at 05:35
  • Actually , i am trying to send data from multiple files to a single file in azure data lake service parallelly, and i have to do this via buffers, but have this restriction that i can only use 5 buffers at max. Now i cannot increment variables or keep counters as this is prallell , so thought of generating some random number btw 0 to 5 , and use it as buffer counter – kchahat20 Jun 30 '20 at 05:38
  • Why not round robin it? – ProgrammingLlama Jun 30 '20 at 05:38
  • 3
    Anyway, as I was typing: say you can run 6 parallel loops. Forget that `Random` isn't thread-safe for the moment and let's just assume it is. You have 6 parallel loops generating random numbers, and you want to make sure that the previously number generated isn't the same as the one you just generated. That means you have to synchronise the 6 loops in such a way that you know which order the occurred in and check that you don't have duplicates. You therefore lose any benefits of doing it in parallel. – ProgrammingLlama Jun 30 '20 at 05:39
  • 2
    Anyway, I feel like you're moving the goalposts of this question significantly. I suggest choosing an answer for this question and then opening a new one about your actual question. Though I think the best solution is a round robin approach. – ProgrammingLlama Jun 30 '20 at 05:40
  • i asked a separate question for this (https://stackoverflow.com/questions/62651706/generate-random-numbers-parallelly-such-that-no-two-consecutives-are-same) , not sure how to implement round robin here. Can u pls tell that over there ? – kchahat20 Jun 30 '20 at 06:46