How to make a random number in CLI/C++? [closed] - winforms

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I want to make a Random number and contain it in an Integer... Here is my current code
Random RandString = gcnew Random();
random = (2 * RandString.Next(1 / 2, 100 / 2));
if (random >= 1 || random <= 35)
{
Friend = 1;
FriendName = "RainFall";
}
if (random >= 36 || random <= 65)
{
Friend = 2;
FriendName = "TempoDrop";
}
if (random >= 66 || random <= 99)
{
Friend = 3;
FriendName = "HeartFelt";
}
if (random == 100)
{
Friend = 4;
FriendName = "SwagMasta";
}
But whenever I try this, I get this error...
1>------ Build started: Project: Retaliation, Configuration: Debug Win32 ------
1> Form2.cpp
1>c:\users\devon\documents\visual studio 2010\projects\retaliation\retaliation\Form1.h(358): error C2664: 'System::Random::Random(int)' : cannot convert parameter 1 from 'System::Random ^' to 'int'
1> No user-defined-conversion operator available, or
1> There is no context in which this conversion is possible
1> Retaliation.cpp
1>c:\users\devon\documents\visual studio 2010\projects\retaliation\retaliation\Form1.h(358): error C2664: 'System::Random::Random(int)' : cannot convert parameter 1 from 'System::Random ^' to 'int'
1> No user-defined-conversion operator available, or
1> There is no context in which this conversion is possible
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What am I doing wrong? What can I do to fix this?

When you use the gcnew operator, it returns a reference so you just have to add the ^ like this
Random^ RandString = gcnew Random();

Related

Discord.js Number Abbreviate (1230 = 1.2k) with economy (quick.db) [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
Much like the Stackoverlow reputation rounding, I'm hoping to do the same thing with currency
$1,000 => 1k
$1,000,000 => 1m
How can I achieve this in JavaScript (preferably in jQuery)?
Here is a simple function to do it:
function abbrNum(number, decPlaces) {
// 2 decimal places => 100, 3 => 1000, etc
decPlaces = Math.pow(10,decPlaces);
// Enumerate number abbreviations
var abbrev = [ "k", "m", "b", "t" ];
// Go through the array backwards, so we do the largest first
for (var i=abbrev.length-1; i>=0; i--) {
// Convert array index to "1000", "1000000", etc
var size = Math.pow(10,(i+1)*3);
// If the number is bigger or equal do the abbreviation
if(size <= number) {
// Here, we multiply by decPlaces, round, and then divide by decPlaces.
// This gives us nice rounding to a particular decimal place.
number = Math.round(number*decPlaces/size)/decPlaces;
// Handle special case where we round up to the next abbreviation
if((number == 1000) && (i < abbrev.length - 1)) {
number = 1;
i++;
}
// Add the letter for the abbreviation
number += abbrev[i];
// We are done... stop
break;
}
}
return number;
}
Outputs:
abbrNum(12 , 1) => 12
abbrNum(0 , 2) => 0
abbrNum(1234 , 0) => 1k
abbrNum(34567 , 2) => 34.57k
abbrNum(918395 , 1) => 918.4k
abbrNum(2134124 , 2) => 2.13m
abbrNum(47475782130 , 2) => 47.48b
Demo: http://jsfiddle.net/jtbowden/SbqKL/
var floor=Math.floor, abs=Math.abs, log=Math.log, round=Math.round, min=Math.min;
var abbrev = ['k', 'Mil', 'Bil']; // abbreviations in steps of 1000x; extensible if need to edit
function rnd(n, precision) {
var prec = 10**precision;
return round(n*prec)/prec;
}
function format(n) {
var base = floor(log(abs(n))/log(1000));
var suffix = abbrev[min(abbrev.length-1, base-1)];
base = abbrev.indexOf(suffix) + 1;
return suffix ? rnd(n/1000**base,2)+suffix : ''+n;
}
Demo:
> tests = [-1001, -1, 0, 1, 2.5, 999, 1234,
1234.5, 1000001, 10**9, 10**12]
> tests.forEach(function(x){ console.log(x,format(x)) })
-1001 "-1k"
-1 "-1"
0 "0"
1 "1"
2.5 "2.5"
999 "999"
1234 "1.23k"
1234.5 "1.23k"
1000001 "1Mil"
1000000000 "1Bil"
1000000000000 "1000Bil"

Kotlin recursively get size of a folder [closed]

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 3 years ago.
Improve this question
I have seen how to return size of a folder in Java: Get size of folder or file, but I couldn't find it in Kotlin.
How do I get the size of a folder in Kotlin?
To get the total size of files in the directory and its children, recursively, you can use the .walkTopDown() function to build a sequence that enumerates all of the files, and then sum the .lengths of its file elements.
val directory: File = ...
val totalSize =
directory.walkTopDown().filter { it.isFile }.map { it.length() }.sum()
Filtering the elements using .isFile is needed here because it is unspecified what .length returns when called on a File denoting a directory.
So here is how to do it :
private fun dirSize(dir: File): Long {
if (dir.exists()) {
var result: Long = 0
val fileList = dir.listFiles()
for (i in fileList!!.indices) {
if (fileList[i].isDirectory) {
result += dirSize(fileList[i])
} else {
result += fileList[i].length()
}
}
return result
}
return 0
}
And if you want a readable string your can do this :
private fun getStringSize(size: Long): String {
if (size <= 0)
return "0MB"
val units = arrayOf("B", "KB", "MB", "GB", "TB")
val digitGroups = (Math.log10(size.toDouble()) / Math.log10(1024.0)).toInt()
return DecimalFormat("#,##0.#").format(size / Math.pow(1024.0, digitGroups.toDouble())) + " " + units[digitGroups]
}
How to use it :
val directory = File(filesDir.absolutePath + File.separator + DIRECTORY_NAME)
println(getStringSize(dirSize(directory)))
Hope it's will help some of you.

Eigen3 : trying to allocate a cube matrix : execution failure

I define a "cube" variable made of 4 nxm dynamic matrix.
Then I define a variable of "cube" type providing the nxm dimensions.
Compilation is ok but I get a crash # exe.
May I ask you some hints please ?
Regards
Sylvain
Here is the code :
typedef Matrix<Matrix<scomplex, Dynamic, Dynamic>, 4, 1> aCube;
aCube myCube = aCube( 15, 12);
Here is the ouput :
tstEigen: /usr/local/include/Eigen/src/Core/PlainObjectBase.h:285:
void Eigen::PlainObjectBase::resize(Eigen::Index,
Eigen::Index) [with Derived =
Eigen::Matrix, -1, -1>, 4, 1>;
Eigen::Index = long int]: Assertion `(!(RowsAtCompileTime!=Dynamic) ||
(rows==RowsAtCompileTime)) && (!(ColsAtCompileTime!=Dynamic) ||
(cols==ColsAtCompileTime)) && (!(RowsAtCompileTime==Dynamic &&
MaxRowsAtCompileTime!=Dynamic) || (rows<=MaxRowsAtCompileTime)) &&
(!(ColsAtCompileTime==Dynamic && MaxColsAtCompileTime!=Dynamic) ||
(cols<=MaxColsAtCompileTime)) && rows>=0 && cols>=0 && "Invalid sizes
when resizing a matrix or array."' failed.
Eventually, chtz put the bug in my ear. See below the working code :
typedef Matrix<Matrix<scomplex, Dynamic, Dynamic>, Dynamic, 1> aCube;
aCube myCube = aCube( 5);
for ( int i = 0; i < myCube.rows(); i++)
{
myCube(i) = Matrix<scomplex, Dynamic, Dynamic>::Zero(4,2);
}
Thanks so much for helping.

How can I apply a low-shelving filter using Visualdsp++?

I'm very new to DSP. And have to solve the following problem: applying the low shelving filter for an array of data. The original data is displayed in fract16 (VisualDSP++).
I'm writing something as below but not sure it's correct or not.
Does the following code have any problem with overflow?
If 1 is true, how should I do to prevent it?
Any advice on this problem?
fract16 org_data[256]; //original data
float16 ArrayA[],ArrayB[];
long tmp_A0, tmp_A1, tmp_A2, tmp_B1, tmp_B2;
float filter_paraA[3], filter_paraB[3]; // correctness: 0.xxxxx
// For equalizing
// Low-Shelving filter
for ( i=0; i<2; i++)
{
tmp_A1 = ArrayA[i*2];
tmp_A2 = ArrayA[i*2+1];
tmp_B1 = ArrayB[i*2];
tmp_B2 = ArrayB[i*2+1];
for(j=0;j<256;j++){
tmp_A0 = org_data[j];
org_data[j] = filter_paraA[0] * tmp_A0
+ filter_paraA[1] * tmp_A1
+ filter_paraA[2] * tmp_A2
- filter_paraB[1] * tmp_B1
- filter_paraB[2] * tmp_B2;
tmp_A2 = tmp_A1;
tmp_B2 = tmp_B1;
tmp_A1 = tmp_A0;
tmp_B1 = org_data[j];
}
ArrayA[i*2] = tmp_A1;
ArrayA[i*2+1] = tmp_A2;
ArrayB[i*2] = tmp_B1;
ArrayB[i*2+1] = tmp_B2;
}
I don't know what the range is for fract16, just -1 to +1 approx?
The section that stands out to me as possibly generating an overflow is assigning org_data[j] but will be dependent on what you know about your input signal and your filter coefficients. If you can ensure that multiplying filter_paraA[2:0] to signal with values tmp_A2..1 = [1,1,1] is < max(fract16) you should be fine regardless of the 'B' side.
I would recommend adding some checks for overflow in your code. It doesn't necessarily have to fix it, but you would be able to identify an otherwise very tricky bug. Unless you need absolute max performance I would even leave the check code in place but with less output or setting a flag that gets checked.
macA = filter_paraA[0] * tmp_A0 + filter_paraA[1] * tmp_A1 \
+ filter_paraA[2] * tmp_A2;
macB = filter_paraB[1] * tmp_B1 - filter_paraB[2] * tmp_B2;
if((macA-macB)>1){
printf("ERROR! Overflow detected!\n");
printf("tmp_A[] = [%f, %f, %f]\n",tmp_A2,tmp_A1,tmp_A0);
printf("tmp_B[] = [%f, %f]\n",tmp_B1,tmp_B0);
printf(" i = %i, j = %i\n",i,j);
}

How to remove bullets on collision in a shooter game?

I'm trying to create a flash shooter game as my first project. But I can't remove the bullets and enemies when they are hit or off-screen.
I've searched for a solution on the problem multiple times and copied about 4 of them (plus I've tried my own ideas) but they are not working.
The current method of checking for collisions is:
for each(var enemy:Enemy in basicEnemies)
{
for each(var projectile:Projectile in bullets)
{
if (projectile.x > enemy.x - enemy.width / 2 &&
projectile.x < enemy.x + enemy.width / 2 &&
projectile.y > enemy.y - enemy.height / 2 &&
projectile.y < enemy.y + enemy.height / 2)
{
trace("collision!");
enemy.enemyHealth = enemy.enemyHealth-5;
projectile.projectileIsPassive = true;
}
}
My question is: How can I remove the enemies and bullets? removeChild and splice?
I'd be grateful if this question got answered.
Yes, you would perform a removeChild and splice them off your container lists. removeChild(projectile);...

Resources