How do I create a strong 64 bit number? [duplicate] - random

This question already has answers here:
How do you create a random 64 bit number? [closed]
(2 answers)
Closed 9 years ago.
Does anyone know how I can create a method that generates a random 64 bit number (0 to 2^63 - 1) that is converted to a character string?
Thanks

In C# you might try something like this:
byte[] bytes = new byte[64];
Random rnd = new Random();
rnd.NextBytes(bytes);
var str64 = Convert.ToBase64String(bytes);

Related

loop always returns same random integer [duplicate]

This question already has answers here:
Generating identical random numbers in sequence after time seed? (Running on my machine)
(2 answers)
Closed 2 years ago.
I've built a function that should return a random key from a array. When it runs it is always returning the same value.
for i := 0; i < 10; i++ {
rand.Seed(time.Now().Unix())
keyArray := [10]string{"key0", "key1", "key2", "key3", "key4", "key5", "key6", "key7", "key8", "key9"}
// fmt.Println(keyArray)
fmt.Println(keyArray[rand.Intn(len(keyArray))])
// var key = null
}
How can i fix this issue?
Don't seed the random number generator inside the loop. Seeding should be done only once. It sets the random number generator into a defined state depending on the seed value. Since the time does not change so fast you will get the same random number almost every time.

In Ruby, how can I save float value with only 1 digit after the decimal point? [duplicate]

This question already has answers here:
Ruby: Rounding float in Ruby
(9 answers)
Closed 2 years ago.
I have a variable named "rating" of type float. It has many digits after the decimal point.
I want to save it to have only one digit. How can I do it?
rating = 4.80999994277954
I would like it to be
rating = 4.8
You can use Float#round to round (rather than truncate) your Float to the precision you want. For example:
rating = (4.80999994277954).round(1)
#=> 4.8
Got it.
rating = rating.round(1)

How to create a hack proof unique code [closed]

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 am creating bunch of unique codes in order to run a promotional campaign.
The campaign will run for a total of 20 million unique items. The validity of the code will be one year. I am currently looking for best possible option.
I can use only 0-9 and A-Z in the code. so that limits me to using 36 unique characters in my code. The end user will need to key in the unique cd in the system and get offers. The unique code will not be tied against any user or transaction to begin with.
One way to generate unique code is create incremental numbers and then convert them to base36 to get a unique cd. The problem with this is that its easily hackable. Users can start inserting unqiue cd in incremental fashion and redeem offers not meant for them. I am thinking of introducing some kind of randomisation. Need suggestions regarding the same.
Note - The limit of max characters in the code is 8.
Use a cryptographically strong random number generator to generate 40-bit numbers (i.e. sequences of 5-byte random arrays). Converting each array to base-36 will yield a sequence of random eight-character codes. Run an additional check on each code to make sure that there are no duplicates. Using a hash set on the converted strings will let you perform this task in a reasonable time.
Here is an example implementation in Java:
Set<String> codes = new HashSet<>();
SecureRandom rng = new SecureRandom();
byte[] data = new byte[5];
for (int i = 0 ; i != 100000 ; i++) {
rng.nextBytes(data);
long val = ((long)(data[0] & 0xFF))
| (((long)(data[1] & 0xFF)) << 8)
| (((long)(data[2] & 0xFF)) << 16)
| (((long)(data[3] & 0xFF)) << 24)
| (((long)(data[4] & 0xFF)) << 32);
String s = Long.toString(val, 36);
codes.add(s);
}
System.out.println("Generated "+codes.size()+" codes.");
Demo.
Use a Guid (C# code):
string code = Guid.NewGuid().ToString().Substring(0,8).ToUpperInvariant();
Since we have a hexadecimal representation we get digits and the characters a to f. We get 16^8 possible codes which is > 4 billion codes. One every 214 for 20 million codes.
Guid.NewGuid().ToString() yields a string like "6b984c2f-5866-4745-ac34-d5088a56070f". Since the first group has a length of 8 characters we can just take the first 8 chars and convert them to upper case. The result looks like "6B984C2F".
Note that this can yield duplicate codes. We can avoid this like this:
var codes = new HashSet<string>();
while (codes.Count < 20000000) {
string code = Guid.NewGuid().ToString().Substring(0,8).ToUpperInvariant();
codes.Add(code);
}
The HashSet allows you to add an item more than once but always only keeps one of them. (Just as math sets.)
If you want to use the full range of possible values the one-liner from above does not do it. With the whole alphabet plus digits we get 36^8 = ~2.8 * 10^12 possible codes. One every 141,055 for 20 million codes. That's better but still not completely hack proof. You will need to limit the number of entry attempts, use a CAPTCHA etc.
const string Base = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int CodeLength = 8;
const int NumCodes = 20000000;
var random = new Random();
var codes = new HashSet<string>();
var chars = new char[CodeLength];
while (codes.Count < NumCodes) {
for (int i = 0; i < CodeLength; i++) {
int pos = random.Next(Base.Length);
chars[i] = Base[pos];
}
string code = new string(chars);
codes.Add(code);
}

ruby 'best way' to generate 2d array [duplicate]

This question already has answers here:
How to declare an empty 2-dimensional array in Ruby?
(5 answers)
Closed 6 years ago.
I have create empty array of array like below , Is there any other best way to initialize it ?.
arr = [[],[],[],[],[],[],[],[],[]]
I think the best way to achieve it by using Array class.
ex:
Array.new(width){Array.new(height)}
you can also provide width & height value like width = 2 & height = 4
Have you tried something like
arr = Array.new(9) { Array.new }
?

Interpreting "takes as input a sorted list of N integers output will be sorted squared integer list" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
how would you interpret that sentence in order to start coding. do you take it as the user inputs the number into an array or does it come from a text document for example and just scan it.
I already did it for both but I only have one submission so I have to make it count
I think the requirement is very clear - you need to write a function that receives a sorted list of integers, and returns a list of their squares.
E.g., in Java:
public List<Integer> squareList (List<Integer list) {
List<Integer> retVal = new ArrayList<>(list.size());
for (int item : list) {
retVal.add(item * item);
}
Collections.sort(retVal);
return retVal;
}

Resources