0

It might be quite funny but I really didn't know how to search to find the answer for this one.

I always use something like this when I want to join strings "string = "something" + "somethingelse" "

but how to do it with INT? :)

Random r = new Random();
int lvfirst = r.Next(485924948);
int lvsecond = r.Next(39);
int lvdone = lvfirst + lvsecond;
Globals.GlobalInt = lvdone;

I tried doing one long int but it doesn't seem to work it says something about Long so if you can help me how to join this 2 random numbers into 1? I need 1 random number max "48592494839"

Thanks a lot!

MiLady
  • 35
  • 1
  • 8
  • 1
    Check this out: [random-number-in-long-range](http://stackoverflow.com/questions/6651554/random-number-in-long-range-is-this-the-way) – Zbigniew Jul 07 '12 at 12:24
  • 2
    So what you really meant is "I need 1 random number max 48592494839", and the rest is just to ensure that you'll get bad answers? – harold Jul 07 '12 at 12:31

4 Answers4

2

There are two problems with this code:

  1. "Sticking together" two outputs of Next will not give you a random value in the target range. Math does not work with strings.
  2. Instead of returning a result it stores it in a global variable.

If you want a value in the range you specify, use

var result = (long) (r.NextDouble() * 48592494839);

This will still not work for any target range, but it will comfortably meet your specific requirements.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • `485,924,948` is smaller than Int32.MaxValue, which is `2,147,483,647`. – matthewr Jul 07 '12 at 12:32
  • @MatthewRz: I would have sworn I counted one digit more :) Thanks for the heads up, edited to correct. – Jon Jul 07 '12 at 12:33
  • I think this is perfect thank you for the code and for explaining I wasn't aware I had problem with the code =)! – MiLady Jul 07 '12 at 12:36
  • just another quick question now I have `var result = (long)(r.NextDouble() * 48592494839); Globals.GlobalLong = result;` if i like to add up or subtract this value how should I do it? `var addup = result + 25;` will be ok? – MiLady Jul 07 '12 at 12:47
  • 1. this isn't a good way to get random numbers, what you'll get wont have any entropy. 2. concating two random numbers does produce one random number. – AK_ Jul 07 '12 at 12:49
  • @AK_: 1. Reference please? 2. It produces one *unpredictable* number, not one *random* number. If you concat two numbers randomly chosen from the range [1, 10] you will get an unknown result but it most certainly *will not* be a *random* number in the range [11, 1010]. You are clearly wrong here. – Jon Jul 07 '12 at 13:21
  • @Jon 1. for example, you will never get the number 16197498279, nor 9718498967, nor a lot of other results that should arise from numbers that double can't represent. 2. I was partially wrong here... if you take two numbers in [1,99] you will get a random number in the [11,9999] range... but it of course depends on the range... – AK_ Jul 07 '12 at 15:24
  • @AK_: First, that has nothing to do with entropy. Second, you are wrong because `double` [can represent any integer with magnitude <= 2^52](http://en.wikipedia.org/wiki/Double-precision_floating-point_format) which is a number much bigger than your examples. Third, it does not "depend on" anything. Math is math. – Jon Jul 07 '12 at 15:28
  • @Jon it cant represent any real number between 0 and 1 ... regarding the second point you should read my response again. – AK_ Jul 07 '12 at 15:36
  • @AK_: *Nothing* can represent **any** real in [0, 1] because there are infinite reals in that range. Do you have a point? – Jon Jul 07 '12 at 15:45
0

You could do something like this:

Random r = new Random();
int lvfirst = r.Next(485924948);
int lvsecond = r.Next(39);
long lvdone = Int64.Parse(lvfirst.ToString() + lvsecond.ToString());
Globals.GlobalInt = lvdone;

Basically we are converting all of the ints to a string which will append the second number to the end of the first number, then we convert it back to an int.

Hope this Helps!

matthewr
  • 4,679
  • 5
  • 30
  • 40
0

Do you want to add or concatenate your ints ?

If you want to concatenate, you can try:

long lvdone = long.Parse(lvfirst.toString() + lvsecond);

If you want to add them and avoid overflows:

long lvdone = ((long) lvfist) + lvsecond

If you number must be arbitrarily long, try using Decimal instead of long

Julien Ch.
  • 1,231
  • 9
  • 16
-1

Ignoring the random thing... this is the way to concat to numbers...

int a = 124;
int b = 101040507;

int digits = 32;
mask = 0x80000000;
while(a & mask == 0)
{
    digits--;
    mask= mask >> 1;
}


long result = b;
result = result << digits;
result = result | b;
AK_
  • 7,981
  • 7
  • 46
  • 78