+ 3
I do it this way:
#include <cstdlib>
(rand()>0.9*RAND_MAX)
+ 5
Manav Roy
I tried this..
Add values to vector, pick a random value, use it and remove from vector.. Until vector empty. If empty, restart procedure to run infinitely.
https://code.sololearn.com/cQdXebomP67z/?ref=app
+ 4
The standard procedure draw random numbers from a uniform distribution, that means all values are equally likely.
What you could to do is sampling from a non-uniform distribution, for example a normal distribution or a chi-square.
Maybe you could shift the parameters of such a distribution?
For your case, a discreet uniform distribution could do? https://stackoverflow.com/questions/69873685/how-to-randomly-pick-element-from-an-array-with-different-probabilities-in-c
+ 2
Manav Roy
rand() is good for simple things. But it have limits if you are using it as a statistics tool. Here is a fun video I found when reading up on it after seeing your post.
https://m.youtube.com/watch?v=LDPMpc-ENqY#
I tried some code, but not being a statistician, I can't figure out the difference. Perhaps someone can give a better demo and explanation on the difference between using rand() and mt19937?
https://code.sololearn.com/cRjuYQiHai08/?ref=app
+ 2
Manav Roy
for giving preference to certain numbers, Brian 's method is fast and practical.
Jayakrishna🇮🇳 is technically more correct. mt19937?! That made me read up on random numbers 🤓. Learned more than what I would probably use.
Here is my take on the topic.
https://code.sololearn.com/c5NC7jNLfRWZ/?ref=app
+ 2
Manav Roy it is possible to make your own pseudo-random number generator. Here is one that I toyed with as proof of concept, but not rigorously proven:
https://code.sololearn.com/cJjrFCp3GiJe/?ref=app
One of my favorite ideas for true random number generation is that of adding a nuclear particle detector to your PC and measuring time between ticks.
+ 2
Brian
then there was this random number generator based on lava lamps in a Tom Scott youtube video.
https://m.youtube.com/watch?v=1cUUfMeOijg
+ 2
Here is my tries for a challenge to generate shuffling of numbers without using inbuilt methods long ago...
It produces different random numbers and shuffles..
Manav Roy you can pick your idea..
(1 more in my code bits, starts with Shuffle named)
https://code.sololearn.com/cIAAd130J8jb/?ref=app
https://code.sololearn.com/c4MHx92a9CAu/?ref=app
+ 2
The given example is a simple Bernoulli distribution and the answers are overcomplicating the solution for it a lot.
Instead of rand() % 2, you can for example write rand() % 100. Since the random number generator is uniform, each possible result of this modulo operation has a probability of 1%. Print 0 if the random number is < 90, otherwise print 1. Done.
If you want more numbers than 0 and 1, you should look at probability distributions though (or add more if conditions)
+ 1
Learn it. 😉Manoay Ray
To solve advanced topics, you need to use advanced methods. Otherwise its difficult to solve it.
Ok. You can use an array instead of vector. But removing from array is complicated, need mores lines of code.. If you still want use array instead of vector. Only additionally you need is to implement remove method.
edit:
Manav Roy
deleting element way:
https://www.geeksforgeeks.org/delete-an-element-from-array-using-two-traversals-and-one-traversal/amp/
you can fill array with a loop.
shuffle array is :
shuffle( arr, arr+n, rng) ; arr : array, n is size of array
edit:
If it seems weird then hardcode to :
Take a counter to 1s probability.
On below 10, use random generator else just put 0. Like :
if(pb < 10){
number = rand() % 2; // this result either 1 or 0 so maximum, you pick 10 times 1.
pb++;
}
else
number = 0; remaining all 0 output.
Seems very simple this is.
+ 1
Manav Roy it looks like 0.9*RAND_MAX is working.
https://code.sololearn.com/ceo6416C57Kq/?ref=app
You asked how to get 1 instead of 0. Just reverse the logic, else use 0.1 instead of 0.9.
(rand()<=0.9*RAND_MAX)
or
(rand()>0.1*RAND_MAX)
+ 1
Manav Roy
It is basically the same as Brian's method, but with more numbers.
But all this depends on the assumption that the random number generated is uniformly distributed. That is the nitpicking reason for avoiding rand().
+ 1
Manav Roy
yes. You should study Jayakrishna🇮🇳 's code.
It introduced me to mt19937.
There are more methods, but that is already a step up.
One of the code I posted used both rand and mt19937.
I can't find the difference, that's why I posted it, hoping someone with more information can explain.
0
I think this way no. Random result is 50 - 50 probability for 1 or 0.
Take a list of 90% 0's and 10% 1's
Shuffle list. Take random numbers from list without repeating.
There may be too advanced options which I don't know..
0
Manav Roy my technique presumes flat distribution of randomness across the range of values from 0 to RAND_MAX. If the random number is above 90% of the range then it returns 1.
I guess the distribution is not flat. In that case you have to figure out at what percentage that 90% of the numbers appear.
EDIT: From what I can tell, for a normal distribution (bell curve) you want to use 5/6, or 0.833333*RAND_MAX. I tried that and it appears to work at the desired 90% rate.
0
sure:
auto r{ rand()%2 };
auto times{ 3 };
do{
r -= (rand()%2);
if(r<0) r=0;
} while(--times && r);
edit: oops, I didn't read the 90%, but each time there is a chance of 50% to set r to 0.
so 3-4 times should equal 90% if im not mistaken.
0
Plz anyone solve my code
- 4
Can you make a code of getting wifi password



