start app on a different layout each time? - layout

I want the app when it is executed to start a different layout, I have this code that allows me to choose the layout where to start but want it to be random thanks.
myPager.setCurrentItem(0);
PD: excuse my English, not speaking, he used the translator

You can use Java's built-in pseudo random number generator to get a random value in a defined range. Use this to choose a random value for setCurrentItem. For example:
Random r = new Random();
int randomValue = r.nextInt( 5 );
myPager.setCurrentItem( randomValue );
The value you pass to the nextInt method is one more than the maximum integer you want to receive; the example above will return a random value between 0 and 4 (excluding 5).

Related

GSL: Random number generator should be unique in one program

I am using GNU Scientific Library to generate random number. Random Number Generation — GSL 2.7 documentation
In general, we should get a gsl_rng firstly.
const gsl_rng_type * T;
gsl_rng * r;
int i, n = 10;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);
But should we use one gsl_rng in one program?
According to the documentation (https://www.gnu.org/software/gsl/doc/html/rng.html, the first paragraph)
Each instance of a generator keeps track of its own state, allowing the generators to be used in multi-threaded programs.
This implies that you can have as many instances of gsl_rng as you wish.

Assign an array to a property in a Chapel Class

Here is a Python-like pattern I need to re-create in Chapel.
class Gambler {
var luckyNumbers: [1..0] int;
}
var nums = [13,17,23,71];
var KennyRogers = new Gambler();
KennyRogers.luckyNumbers = for n in nums do n;
writeln(KennyRogers);
Produces the run-time error
Kenny.chpl:8: error: zippered iterations have non-equal lengths
I don't know how many lucky numbers Kenny will have in advance and I can't instantiate Kenny at that time. That is, I have to assign them later. Also, I need to know when to hold them, know when to fold them.
This is a good application of the array.push_back method. To insert lucky numbers one at a time you can do:
for n in nums do
KennyRogers.luckyNumbers.push_back(n);
You can also insert the whole array in a single push_back operation:
KennyRogers.luckyNumbers.push_back(nums);
There are also push_front and insert methods in case you need to put elements at the front or at arbitrary positions in the array.
I don't think I can help on when to hold them or when to fold them.
A way to approach this that simply makes things the right size from the start and avoids resizing/rewriting the array is to establish luckyNumbers in the initializer for Gambler. In order to do this without resizing, you'll need to declare the array's domain and set it in the initializer as well:
class Gambler {
const D: domain(1); // a 1D domain field representing the array's size
var luckyNumbers: [D] int; // declare lucky numbers in terms of that domain
proc init(nums: [?numsD] int) {
D = numsD; // make D a copy of nums's domain; allocates luckyNumbers to the appropriate size
luckyNumbers = nums; // initialize luckyNumbers with nums
super.init(); // mark the initialization of fields as being done
}
}
var nums = [13,17,23,71];
var KennyRogers = new Gambler(nums);
writeln(KennyRogers);

Guessing game random number to static

I have created a guessing game that generates a random number, takes the user's guess, and basically outputs whether the user's guess is too high or too low(outputs blue, colder) or closer (warmer, outputs red) using both a text box background color and label, as you can see in my code. The issue I am having difficulties with is that every time I click submit, the program generates a new random number. I would like for the program to use the same number until the user's guess is correct, then it can generate a new number if the user would like to play again.
I'm thinking about using a while loop, such as
How could I possible make the random number stay static (same) until it's guessed correctly and if I do need a while loop, where would be the ideal place to place it?
Assign the generated random number to a variable, and then use that variable until you need a new random number.
In this particular instance, the line of code number = rand() % 1000 + 1; needs to be outside of your button click method. Otherwise, every time you click the button, it will generate a new random number.
What you want to do is move your number variable's declaration to the top of the class, outside any functions, like so:
class NumberGuessingGame
{
public: int number = 0;
}
Then, in your MyForm_Load method, you can generate the random number, and set it's value to that variable, like:
private: System::Void MyForm_Load()
{
//Set the value of number here to a newly generated random integer
}
You should then be able to access the number variable inside your button1_Click function:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (input > this->number)
{
//code
}
//Rest of your ifs
}

Why CreateSolidBrush returns random number instead of constant number in c++?

Here is my code :
int clrresult = (int)CreateSolidBrush(RGB(20, 30, 40));
std::wstringstream wss;
wss << clrresult;
Edit_SetText(CLtbx,wss.str().c_str());
every time , I clicked on button , it return rand number with 10 length like this : 1341117845
but the color of the background window is the same. why this happened ?
Why CreateSolidBrush returns random number instead of constant number ?
Thanks.
According the the MSDN documentation, CreateSolidBrush returns an HBRUSH object, which is a handle to a brush, not a meaningful value. You aren't meant to use the numerical value of it directly. It's essentially a pointer to the actual brush object which is managed by Windows.

The last challenge of Bytewiser: Array Buffers

The challenge is like that:
Array Buffers are the backend for Typed Arrays. Whereas Buffer in node
is both the raw bytes as well as the encoding/view, Array Buffers are
only raw bytes and you have to create a Typed Array on top of an Array
Buffer in order to access the data.
When you create a new Typed Array and don't give it an Array Buffer to
be a view on top of it will create it's own new Array Buffer instead.
For this challenge, take the integer from process.argv[2] and write it
as the first element in a single element Uint32Array. Then create a
Uint16Array from the Array Buffer of the Uint32Array and log out to
the console the JSON stringified version of the Uint16Array.
Bonus: try to explain the relevance of the integer from
process.argv[2], or explain why the Uint16Array has the particular
values that it does.
The solution given by the author is like that:
var num = +process.argv[2]
var ui32 = new Uint32Array(1)
ui32[0] = num
var ui16 = new Uint16Array(ui32.buffer)
console.log(JSON.stringify(ui16))
I don't understand what does the plus sign in the first line means? And I can't understand the logic of this block of code either.
Thank you a lot if you can solve my puzzle.
Typed arrays are often used in context of asm.js, a strongly typed subset of JavaScript that is highly optimisable. "strongly typed" and "subset of JavaScript" are contradictory requirements, since JavaScript does not natively distinguish integers and floats, and has no way to declare them. Thus, asm.js adopts the convention that the following no-ops on integers and floats respectively serve as declarations and casts:
n|0 is n for every integer
+n is n for every float
Thus,
var num = +process.argv[2]
would be the equivalent of the following line in C or Java:
float num = process.argv[2]
declaring a floating point variable num.
It is puzzling though, I would have expected the following, given the requirement for integers:
var num = (process.argv[2])|0
Even in normal JavaScript though they can have uses, because they will also convert string representations of integers or floats respectively into numbers.

Resources