1

I know how to choose random numbers between two numbers. However I don't know how to make it to choose a random number that I tell it.

This is what I am trying to do. I have 5 integers.

int Hamburger = 5;
int Sandwich = 7;
int ChickenSalad = 10;
int Pizza = 15;
int Sushi = 20;

5,7,10,15,20 are the prices of each food and I want to make it so that it would choose a random number from these chosen numbers. 5,7,10,15,20.

I am new to C# so I don't really know much about this. I found this

randomNoCorss = arr[r.Next(arr.Length)];

in another post but I don't understand it and I don't know how I can put it in my code.

Stanley S
  • 1,052
  • 13
  • 22
  • I understand you are new to C#. However you may not be new to www.google.com. If you have spent one min on google to search what you are looking for you might find every easily. No worries. Next time try to do some workaround before you post a question. **[http://stackoverflow.com/questions/2019417/access-random-item-in-list](http://stackoverflow.com/questions/2019417/access-random-item-in-list)** .. So I'm voting for closing this question since it's a duplicate. – Siva Charan Feb 11 '17 at 11:23
  • 1
    Possible duplicate of [Access random item in list](http://stackoverflow.com/questions/2019417/access-random-item-in-list) – Siva Charan Feb 11 '17 at 11:24
  • @SivaCharan It's not a duplicate, per se. OP isn't asking how to randomly get a value from an array. He's asking how to get a random value from a number of given values, and doesn't know that the easiest way to do so is to build an array out of them. – Abion47 Feb 11 '17 at 11:26
  • @Abion47: Refer this answer **[http://stackoverflow.com/a/2019432/500725](http://stackoverflow.com/a/2019432/500725)** which talks about how to find the random number from the list of values. I see that the answer which you posted and the answer which i have given references, both are talking about the same topic. Hence question is a duplicate. – Siva Charan Feb 11 '17 at 11:28
  • @SivaCharan Dude, I know what you're saying, but OP's source of values isn't an array (or a list). The linked possible duplicate doesn't address that fact, as it assumes that the values are already stored in an array. – Abion47 Feb 11 '17 at 11:31
  • @SivaCharan The answer I posted *covers* that the best way to approach the solution is to put the values in an array. Your referenced answer doesn't, so it isn't a complete answer to OP's question. – Abion47 Feb 11 '17 at 11:34
  • @Abion47: Refer this question **[http://stackoverflow.com/questions/14297853/how-to-get-random-values-from-array-in-c-sharp](http://stackoverflow.com/questions/14297853/how-to-get-random-values-from-array-in-c-sharp)** and other answers are **[http://stackoverflow.com/a/2019428/500725](http://stackoverflow.com/a/2019428/500725)** and **[http://stackoverflow.com/a/26779169/500725](http://stackoverflow.com/a/26779169/500725)** – Siva Charan Feb 11 '17 at 11:44
  • @SivaCharan Do you not understand what I am saying or something? Are you even reading my comments? Those linked answers *still* assume the source of the values is an array, which is not the case here. – Abion47 Feb 11 '17 at 11:47

4 Answers4

8

You have to create an array of your possible values and then randomly generate an index for that array:

int Hamburger = 5;
int Sandwich = 7;
int ChickenSalad = 10;
int Pizza = 15;
int Sushi = 20;

Random r = new Random();
var values = new[] { Hamburger, Sandwich, ChickenSalad, Pizza, Sushi };
int result = values[r.Next(values.Length)];

What this does is it takes all of your given values and places them inside an array. It then generates a random integer between 0 and 4 and uses that integer to get a value from the array using the generated integer as the array's index.

Abion47
  • 22,211
  • 4
  • 65
  • 88
1

Full code is:

Random r = new Random();
int[] priceArray = new int[] { 5, 7, 10, 15, 20 };
int randomIndex = r.Next(priceArray.Length);
int randomPrice = priceArray[randomIndex];
Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
0

You need to add your values in an array and then you can choose a random number from that array

  int[] myNumbers = new int[] { 5, 7, 10, 15, 20 };
  var random = new Random();
  var numberResult = myNumbers[random.Next(5)];
Stanley S
  • 1,052
  • 13
  • 22
-1

You can do this in LINQ:

int[] intArray = new int[] { 5, 7, 10, 15, 20 };

int result = intArray.OrderBy(n => Guid.NewGuid()).Select(x => x).Take(1)
            .SingleOrDefault();

The result will be random based on your declared array of integers in variable intArray.

Or you can do this by getting the random index of your array:

 int[] intArray = new int[] {5, 7, 10, 15, 20 };
 Random rndom = new Random();
 int index = rndom.Next(0, intArray.Length - 1); //Since int array always starts at 0.
 int intResult = intArray[index];

Let me know if you need more clarifications.

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
  • Never use GUIDs as a function of randomness. A GUID is guaranteed to be unique, not random. Use `Random.Next` instead. – Abion47 Feb 11 '17 at 11:28
  • 1
    Have you tried the code first? It will generate a random number based on the array selected, and not beyond it. – Willy David Jr Feb 11 '17 at 11:30