Questions tagged [srand]

a function which initializes the pseudo-random number generator in C/C++ or PHP

A random seed is used to determine the initial state of a pseudo-random number generator. Such generators are used in many programming languages.

For a specific generator and a single value of seed the sequence of generated numbers will be the same.

Sample code:

#include <cstdlib>

int seed = 111;
int arr1[10];
int arr2[10];
srand (seed);
for (int a=0; a<10; a++) {
  arr1[a] = rand();
}
srand (seed);
for (int a=0; a<10; a++) {
  arr2[a] = rand();
}
for (int a=0; a<10; a++) {
  assert arr1[a] == arr2[a];
}
396 questions
0
votes
3 answers

How to change srand() at runtime

I'm using srand() with a fixed seed and I need to run tests with a set of different seeds like 100, 200, 300, ..., 1000 all in one execution. Is this possible? The thing is srand() is defined at the beginning of main, so I don't know how to control…
Pablo Acuña
  • 77
  • 1
  • 8
0
votes
1 answer

MS VS 2010 srand() not compiling?

I have a piece of code ( I'm student ) that "should" work in theory, but Microsoft's Visual Studio 2010 seems to have a problem with srand, because it's not being highlighted like other reserved names. If I remove srand from Auto_Complete_Matrix…
JorgeeFG
  • 5,651
  • 12
  • 59
  • 92
0
votes
4 answers

quicksort for integers, segmentation fault in rand()

I'm writing a quicksort algorithm for integers and I get a strange segfault error in srand functions. Here is the code from sort.h: int distributePivot (int *a, int left, int pivot, int right) { int i, j; if (pivot != right) …
Zagorax
  • 11,440
  • 8
  • 44
  • 56
0
votes
2 answers

Random Number Generator with a Seed

I am generating random numbers by using srand(time(NULL)). Any idea why it always gives just even random numbers? In my case its giving so. Please help i need odd numbers too. I need the set of 0s, 1s. for eg : {1,1,0,0,1,0,0,0,1,1,0}
Arush Kamboj
  • 673
  • 3
  • 10
  • 20
-1
votes
1 answer

What techniques can I use to determine the number generated by srand() in C?

How to know the secret number from srand((uint32_t)timer) where time_t timer = time(NULL) #include #include #include #include void printTerminalTime(time_t t) { char buffer[32]; struct tm* tm_info =…
Allen
  • 1
-1
votes
2 answers

Why do I get the same number when using rand/srand in a function before main?

Why am I getting a different random number each time I run my code in main, but when I run it in a function I am getting a static number? (1) Random number in Main... int main() { srand(time(0)); int loot = 1+(rand()%9); cout << loot; …
-1
votes
1 answer

RockPaperScissor game not getting expected result - C programming

I was writing c program on rock paper and scissor game but could not get the expected result. #include #include #include int main() { char player[50], choice[10]; puts("Enter your name: "); gets(player); …
Pallavarv
  • 13
  • 3
-1
votes
2 answers

Random and Different Numbers in C

I am trying to generate 7 numbers in C. The code should contain: It mustn't start with 0. The numbers must be between 0-9 Numbers must be different from each other. (For example: there can't be two 5s, like this one: 7 5 8 3 2 5 4). My code is…
Bolin
  • 1
  • 1
-1
votes
1 answer

C Programming - Random Numbers aren't being generated when I hit output

//My Task is to create a dice throwing program that generates random numbers between 1-6. //I had to use the random numbers to show how many times the dice landed on a number in 60 goes. //After this, I would have to use a loop to generate 10*60 of…
Tee
  • 25
  • 5
-1
votes
1 answer

why does NULL of srand (time (NULL)) give time () a random value?

In C language, use srand (time (NULL)); can produce random numbers from 0 to 32767. My question is, why does NULL of srand (time (NULL)) give time () a random value? Also, how to generate random numbers over 32767? Use if (rand ()% 2 == 0) {ans =…
-1
votes
2 answers

Why does someNumber = rand() & 100 + 1; not produce an error?

I was attempting to generate a random number between (1 - 100), and the code to ran, but not doing what I wanted. I noticed I accidentally type &, instead of % with the rand() function, and the compiler did NOT give me an error. What exactly is…
-1
votes
2 answers

Generating array with random integers C

so I am trying to fill an array with random integers. for (int i = 0; i < 2; i++){ srand(time(NULL)); array[i]=rand()%30; }` Here is my code so far. I am currently getting the same random number twice. Would like to know if there is a way…
crooose
  • 111
  • 4
  • 10
-1
votes
2 answers

Fix seed via srand() produces different results from rand()

In my Julia 0.5 script I use srand(1234) to get the same results from rand() each time I re-run the script. However, I get different results. What do I wrong?
JdG
  • 19
  • 4
-1
votes
1 answer

How the memory requirements of the following codes are different irrespective of the code-length?

// An efficient program to randomly select a number from stream of numbers. #include using namespace std; #include #include /* A program to randomly select a item from stream[0], stream[1], .. stream[i-1]*/ int…
scoder
  • 3
  • 5
-1
votes
2 answers

How to store 00 in an array?

I store four numbers in my array, 00,11,22,33. When I generate a random number and print it, it displays 0 rather than 00 (when the first element is selected). The other numbers are fine and displaying correctly. How can I store 00 in an array so…
butbut
  • 31
  • 3