Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to get the whole number of this random.
Finally, I get a Debug.Log () to display my 757 number
You need to build up the whole number:
Random random = new Random();
int hundred = 100 * random.Next(1, 10);
int ten = 10 * random.Next(0, 10);
int one = random.Next(0, 10);
int result = hundred + ten + one;
Lets assume, the first random gives you 7, the second gives you 5 and the third gives you 7 again. you would end up with:
hundred = 100 * 7 = 700
ten = 10 * 5 = 50
one = 5
result = 700 + 50 + 7 = 757
Then just log result
What you describe is random 3 digit number. So simply:
Random.Range(100, 1000);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
static int Sss()
{
int k = int.Parse(Console.ReadLine());
int[] numbers = new int[k];
numbers = GenerateRandomNumbers(numbers);
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] < 10 || numbers[i] > 99)
{
Console.WriteLine(numbers[i]);
I need to do a method, Ssk(k), which generates k random numbers and returns the product of these numbers which are double digits and ends with 5.
A non-negative integer x ends in 5 when written as a decimal if and only if x % 10 == 5 is true.
I note that you are returning an int, but ints can only go up to about two billion. The product of five two-digit numbers is almost certainly over that. You should use long, decimal, double or BigInteger instead, depending on your use case.
For any number you can check what the last digit is using by modulo operation, which in C# is a %, but works ONLY for integers. In your case you should check if numbers[i]%10==5.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I would like to make my 2 ints get together in one number but where the biggest of the 2 get in the front and the other one in the back
I have a var named num1 and one named num2.
The numbers of those 2 are getting assigned thru a random.
I would like to make them into one number but with the highest number first and the other one after it. I don't want to + them together but make a big number.
For example num1 = 5 and num2 = 6 my whole number should then be 65.
Thx in advance I tried googling this but I could not really find what I was. Looking for sry for bad English
Utilise Math.Max and Math.Min to find the largest and smallest of the two numbers, then concatenate and parse to an int.
int result = int.Parse(Math.Max(num1, num2) + ""+ Math.Min(num1, num2));
or if the number can get large after concatenation then use the long data type.
long result = long.Parse(Math.Max(num1, num2) + ""+ Math.Min(num1, num2));
It sounds like you wish to randomly generate the digits separately, then combine the digits to form a two-digit number. So:
var num1 = 5;
var num2 = 6;
var bigNumber = num1 + 10 * num2; //65
I found a solution i used a little of both of your answers
This is what it ended like
num1 = randomNum.Next(1, 7);
num2 = randomNum.Next(1, 7);
maxNum = Math.Max(num1, num2);
minNum = Math.Min(num1, num2);
wholeNum = minNum + 10 * maxNum;`
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've been trying to make a web app that converts seconds to hours/minutes/seconds but with no luck. I can't figure out how to do this at all, i created over a dozen c# projects but none works with any code i try and i also do not understand the creation of such thing. I would greatly appreciate it if someone could help me create this form.
int seconds = 1000;
TimeSpan timespan = TimeSpan.FromSeconds(seconds);
int hour = timespan.Hours;
int min = timespan.Minutes;
int sec = timespan.Seconds;
You can use simple aritmetic, for example:
int hours;
int minutes;
int seconds;
seconds = 12351;
hours = (int)(Math.Floor((double)(seconds / 3600)));
seconds = seconds % 3600;
minutes = (int)(Math.Floor((double)(seconds / 60)));
seconds = seconds % 60;
string time = hours + ":" + minutes + ":" + seconds;
If there should be a leading zero on the hours, the final line can also be changed to
string time = (hours.ToString().Length == 1 ? "0" + hours : hours) + ":" + minutes + ":" + seconds;
var timeSpan = TimeSpan.FromSeconds(500000);
var totalMinutesInTimeSpan = timeSpan.TotalMinutes;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a table that stores the amount of RAM a server has in a biginit column with values such as 2470208.
But how I can apply a data annotation or other validations to show only 2 instead of s470208. ?
I mean to always divide by 1 million and get the number on the left side of the digit ?
1) Use this for automatic thousands-unit:
string GetByteString(long n) {
int k=0;
string u=" kMGTEP";
while(n>1024) {
n>>=10;
k++;
}
return n.ToString() + u[k];
}
Call:
string s= GetByteString(1234567890123);
Debug.WriteLine(s);
2) But if you simply always want MB just shift by 20:
long n = 123456789;
string MB = (n>>20).ToString();
But this will show 0 if n goes below 1MB.
Reason:
1 kB = 2^10 = 1<<10 = 1024;
1 MB = 2^20 = 1<<20 = 1024*1024 = 1048576;
1 GB = 2^30 = 1<<30 = 1024*1024*1024 = 1073741824;
You tagged C# but mentioned a bigint column so it isn't clear whether you're looking for a database or C# solution. The following C# method will take the number of bytes as an integer and return a formatted string...
public string FormattedBytes(long bytes)
{
string units = " kMGT";
double logBase = Math.Log((double)bytes, 1024.0);
double floorBase = Math.Floor(logBase);
return String.Format("{0:N2}{1}b",
Math.Pow(1024.0, logBase - floorBase),
units.Substring((int)floorBase, 1));
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a quiz that have many questions and 5 possibilities of answers.
Lets take one question,it has follow answers:
148 - Good
5 - N/A
268 - Great
5 - Regular
11 - Bad
These are numbers collected directly from database.Now i need to show it as percentage.i.E:
Great - 45%
Good - 40
[..]
and so on
Any ideas?
int na = 5;
int good = 148;
int great = 268;
int regular = 5;
int bad = 11;
int sum = na + good + great + regular + bad;
naPercent = getPercent(na,sum);
float getPercent(int value, int sum)
{
return (value*100.0)/sum;
}
This is not a programming question, it is a math question. The percentage of each item is equal to the number of that item divided by the total number. In your example, the total number is 148+5+268+5+11 = 437. Great = 268 / 437 = 61.327%
total count for the answer / total count for all answers to this question combined * 100