hii all ,
I need to assign either 1 or -1 randomly to an 3 dimensional array
where 1 corresponds to orange and -1 corresponds to blue
can someone help me in C++ code
Thank You,
chin
hii all ,
I need to assign either 1 or -1 randomly to an 3 dimensional array
where 1 corresponds to orange and -1 corresponds to blue
can someone help me in C++ code
Thank You,
chin
I can think of a couple ways, but the best one depends on your answers to these questions:
why not use a 3d bool array to save space if you are only allowing two values anyway?
Look at this link for information on random number generation.
As you are only looking for 2 values you can just use
if(rand() % 2 == 0) value = true; //or 1 or whatever you want
Obviously loop this command through the entire array
Colezy
yes there are particular reasons
1)It needs to be either 1 or -1 since 1 represents orange and -1 represents blue
which values i have to use later in the program
2)it shouln't 0 or 1
3) no
4)NA
ok then so...
if(rand() % 2 == 0) {value = -1;}
else {value = 1;}
In 3 nested 'for' loops.
Colezy
why not use a 3d bool array to save space if you are only allowing two values anyway?
There will be a 3 dimensional array (which should be a cube )in that my elements (which are spheres either orange or blue)
should have the values either 1 or -1 for each
Use rand() modulo 2 as the condition for an if. Which value you store will depend on if the produced value is 0 or 1.
if(rand() % 2 == 0) {value = -1;}
else {value = 1;}
Just make sure you seed the RNG first. Otherwise, you'll get an identical pattern every time.
Just make sure your Otherwise, you'll get the same pattern every time.
can u please
give me an example on that
Thank you
You don't seem to have the whole post. Did you follow the link? There is an example on the linked page. But it's basically this:
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand((unsigned)time(0));
//begin loop1
//begin loop2
//begin loop3
if(rand() % 2) {value = -1;}
else {value = 1;}
//end loop3
//end loop2
//end loop1
return 0;
}
EDIT:
IDK what you did with your quote tags, but I've gotta remove them.
Ah yes of course, good point Fbody.
Seed using a value such as:
srand(time(NULL));
Colezy
can u please
give me an example on thatThank you
You don't seem to have the whole post. Did you follow the link? There is an example on the linked page. But it's basically this:
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand((unsigned)time(0));
//begin loop1
//begin loop2
//begin loop3
if(rand() % 2 == 0) {value = -1;}
else {value = 1;}
//end loop3
//end loop2
//end loop1
return 0;
}
ok, thank you
and Can u help me in later part of the code
since i have to calculate the sum of all the neighbour( 6 neighbour for every element wrap around will be considered) values(i;e 1 or -1 )
if the sum of the value(value of each neighbour) is 0 or -1 i have to change the color (change to color with majority of neighbours color)
if it is 1 need not to change
i have to do it for every element in the array and flip the color of each element if necessary to orange or blue(the colors used)
Thank you
That seems pretty straight forward to me.
How would you do it? I would advise writing an algorithm out on scratch paper so that you can think it through properly.
I would also advise you to use a function for it, but that's not my call.
EDIT:
By the way, you're doing something funky with your code tags. I don't know exactly what it is, but it's breaking them and causing the quotes to flop posters. I suspect you are erasing something you really shouldn't.
Do anyone know how to calculate the sum of the values around a particular element
(for every element 6 neighbours will be there{wrap around will be used})
Thank You
Do anyone know how to calculate the sum of the values around a particular element
(for every element 6 neighbours will be there{wrap around will be used})
Thank You
Don't be lazy, take some time to think about it. Then share what you came up with.
Here's a hint: It'll involve variables for adjacentTotal, currentX +/- 1, currentY +/- 1, and currentZ +/- 1. You will also need another set of loops and another if.
Don't be lazy, take some time to think about it. Then share what you came up with.
ok
I will try to come up with something
if the sum of the value(value of each neighbour) is 0 or -1 i have to change the color (change to color with majority of neighbours color)
if it is 1 need not to change
What if it is not -1, 0 , or 1. Do you mean if it is <= 0 change?
Start to think of the problem in 1D. The solution can be scaled up from there.
If you show you are making an effort to make your own algorithm then we will contribute.
Colezy
What if it is not -1, 0 , or 1. Do you mean if it is <= 0 change?
Start to think of the problem in 1D. The solution can be scaled up from there.
If you show you are making an effort to make your own algorithm then we will contribute.Colezy
yes when <=0 we have to change
Just coming back to the assignment of a value. I think this is a little verbose and confusing:
if(rand() % 2 == 0) {value = -1;}
else {value = 1;}
Can we use the mathematical property that you can linearly map any number set to any other smaller/equally sized number set, and in this case : value= 2*(rand() % 2)-1;
The advantage is to avoid the conditional, and that avoids a lot of cpu time.
[Actually : with full optimization, g++ converts this to equivalent code, however, that is not what happens in a two conditional case.]
This kind of construction comes up a lot, it is important to know it because you will see it in other peoples code, and when you decide that you want to modify it to give your values between -6 and 8, it is easy to change, were as the if construction is not. E.g. value=(rand()%15)-6;
Sorry for the interjection but with three layers of loop, you have saved yourself a layer of confusion. [at the cost of a slightly more complex construction]
So in short, fix your nearest neighbour sum first.
Actually, the code suggested to OP isn't quite good. As you guys probably know the random distribution of rand() is one of the top, but usually is good enough. But by using the modular arithmetic, especially when the range is low, causes the random distribution to be even worse. So you can use the if(rand() % 2 == 0){...}
as suggested, but if you want something thats a little slightly better then do something like this if(rand() > ceil(RAND_MAX/2.0f) ) {...}
. The reason why this is better is because its first-half range is maximized and its second-half range is also maximized. Thus you have a bigger gap, and spread the distribution more evenly. Its just a suggestion. You don't have to follow it.
I always thought that using a short span with modulus was the most effective way as according to cplusplus.com the rand() is slightly more likely to give a lower number than a higher one. So by taking the modulus 2 of the number we are only looking at whether the number returned was odd or even. We don't care if it was high or low, thus eliminating the slight bias towards low numbers.
Do correct me (again) if I'm wrong though.
P.S. I do like your formula StuXYZ :P
Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans
Its talking about when one uses the modulus operator. It is not talking about rand() in general. rand() in general is uniform.
Oh dear... I misread my own link, how embarrassing.
I still don't understand the maths that would cause a 0 to be slightly more likely than a 1 in rand()%2, can you explain?
The only thing I can think of is that because the RAND_MAX constant might be an odd or an even number we will either have an equal number of odds and evens or 1 more even number. However, this slight difference would be SO insignificant that it would be unnoticeable. For example we could be talking about the difference between 16383 odds and 16384 evens.
With the standard implementation of RAND_MAX being 32767 there is an equal number of both.
Can anyone shed any light on this?
Cheers,
Colezy