Good afternoon!

I seem to disappear from this forum for a while then re-appear...I've been so busy with my first born baby girl that I haven't had time to check my email much less work on any of my projects, but something has come up and I need some input.

I have an application that uses the built in random function to generate a random number. I'm wondering, if I need the result of the function to conform to a standard (namely being an integer, not a decimal) is it permissible to multiply the result by say 10,000,000 and then "round" the result (to lose the decimal) of the random function and have it still maintain its randomness?

I'm asking because something has happened with my application that should not happen in trillions of years (literally). I've noticed that when generating random numbers they repeat.

Imagine, for example, the following list of fruit:

  • Apple
  • Orange
  • Peach
  • Strawberry

Now imagine you were going to eat one of these each day and you used a random algorythm to assign each fruit a random number, and then you ate the fruit with the lowest number. Each day you repeat the process. That's essentially what I am doing, but on a scale with thousands of fruit. And in my case, the numbers repeated...like, not just a little, but EXACTLY the same numbers.

I'm wondering if it's my multiplying or rounding that could cause it?

Any ideas?

I've noticed that when generating random numbers they repeat.

It sounds like you want a random shuffle rather than a random selection. The former will give you a randomized ordering of the set such that there are no repeats until the set is exhausted:

// Untested code
var rand = new Random();
var to_eat = from x in fruits
             order by rand.Next()
             select x;

foreach (var fruit in to_eat)
{
    Console.WriteLine("nom nom " + fruit.Name);
}

Random doesn't mean "no number will be repeated", it means the sequence of numbers will be suitably unpredictable.

deceptikon,

No, no...it's actually okay for a selection to be made twice in a row. Each time numbers are assigned, it is very possible for the same item to be the top one on the list.

For example, if I use my list of fruit and assign random numbers to them:

  • Apple (19492)
  • Orange (44813)
  • Peach (1291)
  • Strawberry (189385)

The resulting order would be Peach, Apple, Orange then Strawberry. However if I run the method again I would expect different random numbers and (potentially) a different order, but not necessarily. This would be okay:

  • Apple (58469)
  • Orange (69211)
  • Peach (29599)
  • Strawberry (86910)

The numbers are different, but the order is the same.

However, this would be unexpected:

  • Apple (19492)
  • Orange (44813)
  • Peach (1291)
  • Strawberry (189385)

That's the same set of numbers, same order etc... as last time. The odds of that are...what? Astronomical?

Why do you need to 'multiply and truncate'? Random returns integers by default, you have to ask it for doubles.

Also, with no code, there is no way to determine why you are getting the same sequence.

Edited 2 Years Ago by Ssyxz

Are you using a specific same seed value, every time when you call the constructor?

Comments
Yep! Specifying a seed will give you a deterministic RNG

ddanbe,

I think that might be it. The seed is the same, but varies each time it's run (or is supposed to). I'm going to try that and see if it changes anything. That's the only thing I can think of.

This question has already been answered. Start a new discussion instead.