This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to generate a random number from within a range - C
I'm looking for something I can use in C that will give me a random number between a and b. So something like rand(50) would give me a number between 1 and 50.
From the comp.lang.c FAQ: How can I get random integers in a certain range?
You can use either rand or random to get an arbitrary random value, then you can take the result of that and mod it by the size of the range and then add the start offset to put it within the desired range. Ex:
(rand()%(b-a))+a
You need to use rand() and srand().
http://linux.die.net/man/3/rand
Related
This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 5 years ago.
So I am trying to learn how to divide fractions. I am confused why dividing variables gives the correct result, and dividing the numbers themselves gives an incorrect result. I have tried to searching on here and couldn't find anything relevant. Here is an image to show why I am talking about.
The expression 1 / 3 is an integer expression. You divide two int values. That leads to truncation.
Try e.g. 1.0 / 3.0 instead.
This question already has an answer here:
srand function is returning same values
(1 answer)
Closed 7 years ago.
So I'm generating some random numbers. I'm using
srand(time(NULL));
to generate random numbers. However I have this within a for loop. Im generating 10 'sets' of random numbers, but they are all the same. I was wondering how I could change this line of code to ensure the random numbers each time the program goes round the loop.
Already answerd : What you'll want to do is call srand() once, when the program starts (at the start of your main() function)
Then call rand() every time you want a random number
srand function is returning same values
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 8 years ago.
Improve this question
Given a number how to recognize if it is bleak or supported by some number in efficient manner?
Given an array of numbers, how to check efficiently whether each number is supported with in the
array or bleak if not supported with in the array?
Brute force : Find binary equivalent, count number of 1's and search for it in the array.
About Bleak and supported numbers:
For each number, count the number of ones in its own binary representation, and add this count to itself to obtain the value of the number it supports. That is, if j is the number of ones in the binary representation of m, then m supports m+j.
Example:number eight (1000 in binary) supports nine, whereas nine supports eleven.
However, in this way not all the numbers get supported; some are left without support, and these numbers are called bleak. For example since one supports two, two supports three and three supports five, there is no number less than four, which would support four, so four is bleak.
If n is not bleak, it must be supported by a number in the range n-ceil(log2(n)) to n-1. This gives a very small range you have to check. For the array, first sorting the array then using the same principle should give you an efficient solution.
Denote a as the given array and count(x) as the number of 1 bits in x.
Question 2:
Iterate through the array and save the the number a[i] + count(a[i]) into a binary search tree. Time: O(n log n)
Iterate through the array and output "Supported" if a[i] is in the binary search tree and "Bleak" otherwise. Time: O(n log n)
Note: a[i] is the current element at the iteration.
This question already has answers here:
Quickest way to find missing number in an array of numbers
(31 answers)
Closed 8 years ago.
Can someone explain what this question is asking for?
Array A contains n‐1 unique integers in the range [0,n‐1] and there is one
number from this range that is not in A. Design an O(n) algorithm for finding that
number. You are allowed to use only O(1) additional space besides the array A itself.
Does this question means the length of the array is (for example: 5). And array contains = {0,1,x,3,4}. Find x?
What is O(n) and O(1)? How do I find the missing number in the array
using O(n) algorithm?
Help is appreciated. Thanks.
Does this question means the length of the array is (for example: 5). And array contains = {0,1,x,3,4}. Find x?
Yes. (More or less)
What is O(n) and O(1)?
Read your algorithmics text book. Or your lecture notes. Or http://en.wikipedia.org/wiki/Big_O_notation.
How do I find the missing number in the array using O(n) algorithm?
That would be solving the problem for you!
Hint: what is the formula for the sum of 1 to N?
Does this question means the length of the array is (for example: 5). And array contains = {0,1,x,3,4}. Find x?
not exactly: if n was 5, the array would contain four unique naturals from 0 to 4 (n-1): {3, 1, 4, 0}.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Converting a Uniform Distribution to a Normal Distribution
Hello.
I'd like to know of any algorithm implemented in C which can take a random value between 0 and 1, the mean and standard deviation and then return a normally distributed result.
I have too little brainpower to figure this out for myself right now.
I can't find anything useful on the internet.
Thank you.
Box-Muller is the transform you need.
There's already been the suggestion for Box Muller, but a computationally simpler approach is simply to take advantage of the central-limit theorem; add enough independent random variables together, and the result will approximate a normal distribution.