3

I have a table that I am trying to get a random number from:

//Fruits
id  |  FruitName
----------------
2   |  Banana
3   |  Apple
4   |  Orange
6   |  Grape
7   |  Plum
8   |  Lime
10  |  Kiwi

The problem is because of the inconsistencies (note: ids 1, 5, and 9 are missing), writing the following statement will not work (even if written properly):

Random.Next(someLinq.id).First(), (samelinq.id).Last())

How would I get a random number from the numbers available?

Skullomania
  • 2,225
  • 2
  • 29
  • 65
  • I don’t know the syntax of C#, but can’t you find the length of he table, put the ids in an array numbered 1-length, and get the (randbetween(1, length))th element of the array and get that fruitname? – Samyok Nepal Nov 18 '17 at 00:21
  • Are you using Entity Framework and you want to query this table in database? – AaronLS Nov 18 '17 at 00:22
  • @AaronLS yes I am – Skullomania Nov 18 '17 at 00:23
  • 4
    `Random.Next(0, CountOfYourItems); ` then use `.ElementAt()` –  Nov 18 '17 at 00:26
  • Possible duplicate of [EF Code First: How to get random rows](https://stackoverflow.com/questions/7781893/ef-code-first-how-to-get-random-rows) – AaronLS Nov 20 '17 at 14:39

4 Answers4

7

You can simply use the count of the items as the max random number, and then select a random item by index:

var random = new Random();
var items = new List<int> {2, 3, 4, 6, 7, 8, 10};
var randomItem = items[random.Next(items.Count)];

Or, in your case (and assuming you are getting a list of Fruit items which contain an Id property):

var items = new List<Fruit>
{
    new Fruit {Id = 2, Name = "Banana"},
    new Fruit {Id = 3, Name = "Apple"},
    new Fruit {Id = 4, Name = "Orange"},
    new Fruit {Id = 6, Name = "Grape"},
    new Fruit {Id = 7, Name = "Plum"},
    new Fruit {Id = 8, Name = "Lime"},
    new Fruit {Id = 10, Name = "Kiwi"}
};

var random = new Random();
var randomItem = items[random.Next(items.Count)];

Console.WriteLine("Randomly chose fruit: Id = {0}, Name = {1}", 
    randomItem.Id, randomItem.Name);
Rufus L
  • 36,127
  • 5
  • 30
  • 43
4

you can add all numbers in a array after you can create a random with array's lengt and you can use this number as index of number in the array

int[] array = new int[]{2,3,4,6,7,8,10};
Random rnd = new Random();
int randomIndex = rnd.Next(0,array.length);
var x = array[randomIndex];

And you can do what you want with this x variable

Kadir Kalkan
  • 248
  • 2
  • 10
2

How about something like this?

// get the count
var count = context.Fruits.Count();
// generate a random fruit index
var random = new Random().Next(count);
// skip by random
var randomFruit = context.Fruits
                         .Skip(random)
                         .Take(1)
                         .FirstOrDefault();
trashr0x
  • 6,457
  • 2
  • 29
  • 39
1

For a statitstical and cryptographically absolutely not correct random value you could go with

var randomId = resultOfDbLinqQuery
    .OrderBy(e => Guid.NewGuid())
    .First()
    .Select(item = item.id);
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69