-7

how do I generate random numbers between -32 to 32 or -64 to 64 and excluding the value zero, and duplicates in array? I'm trying to generate it as efficiently as possible

  • 1
    How about generating a number in the range *including* zero, and if you get zero then generate another? – Some programmer dude Nov 09 '18 at 07:29
  • @Someprogrammerdude I was thinking if your random function is uniform, will this method still generates uniform output or not. I mean ignoring unwanted numbers. I think output will not be uniform anymore. – Afshin Nov 09 '18 at 07:31
  • What do you mean by **"and duplicates in array"** ? What array? – selbie Nov 09 '18 at 07:32
  • And please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Nov 09 '18 at 07:34
  • Like I'm trying to put values in to an array by generating random numbers from -64 to 64 without 0 or having duplicates in absolute value; for instance, can't have 64 and -64 inside the array but it's possible to have just have one or the other. – Halbert3425 Nov 09 '18 at 07:34
  • It seems like you want a *set* rather than an array then? Perhaps read more about [`std::unordered_set`](https://en.cppreference.com/w/cpp/container/unordered_set) (or its sibling [`std::set`](https://en.cppreference.com/w/cpp/container/set))? – Some programmer dude Nov 09 '18 at 07:38
  • @All Sorry, I closed to an absolutely rubbish page. There are better "no repeat generator" answers out there on the site. Might someone be able to close to something else? I'll get me coat. – Bathsheba Nov 09 '18 at 08:13
  • @ugandanwarlord: you might have a valid non-duplicate question there, precisely because of the restriction that your output can contain either +64 and -64. But as pointed out before, you need to write your question clearly and completely. Considering that this is an algorithm question, when you ask a new question make sure to include your code that compiles, and then point out why your code fails. We can then help fix that code. – MSalters Nov 09 '18 at 11:09

2 Answers2

2
int signext = (rand() % 2) ? 1 : -1;
int x = (rand() % 64 + 1) * signext;  // random value from -64 to 64, excluding 0
int y = (rand() % 32 + 1) * signext;  // random value from -32 to 32, excluding 0

And it can be done with a single call to rand():

    int x = rand() % 128;
    x = ((x / 2) + 1) * ((x % 2) ? 1 : -1);

    int y = rand() % 64;
    y = ((x / 2) + 1) * ((x % 2) ? 1 : -1);
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Great way of handling the zero case! – Aykhan Hagverdili Nov 09 '18 at 07:33
  • 1
    This answer could be much better if it had an explanation about what the code is doing, and why it's doing like that. Especially the use of `!!` which for beginners looks very weird. – Some programmer dude Nov 09 '18 at 07:33
  • Does it even require the `!!`? Isn't there an implicit cast to `bool` either way? – Blaze Nov 09 '18 at 07:37
  • it does not require `!!`. implicit conversion will be done here. – Afshin Nov 09 '18 at 07:39
  • @Afshin - often time compilers will generate a warning about implicit downcasts. – selbie Nov 09 '18 at 07:40
  • 1
    Removed the `!!` operation in favor of something more readable. – selbie Nov 09 '18 at 07:41
  • that will be compiler problem. because implicit conversion to `bool` from integral types is in standard. quote from standard: *The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.* – Afshin Nov 09 '18 at 07:43
  • 1
    Invoking PRNG 3 times would be inefficient. – Daniel Langr Nov 09 '18 at 07:47
  • In fact, since you only need a 7 bit and a 6 bit value, you can call `rand` once and still have enough entropy. `rand` must provide at least 15 bits of entropy. – MSalters Nov 09 '18 at 11:12
2

It can be simply done by generating random numbers from the range 0,...,127 and then doing some arithmetic:

int r = rand() % 128 - 64;  
if (r >= 0) r++;

This will likely be faster than invoking PRNG multiple times. BTW, I would suggest to employ C++11 random library instead of rand function.

If you also need to check whether the resulting r is in some array, I suggest to sort this array first and then to try to find r inside with binary search. If it is found, simply skip that random number and generate another one.

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93