How to print random numbers from a selected sequence? - arrays

I want the program to print randomly one of these numbers:
{1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10}
Instead it prints randomly nonsense numbers
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand( time(NULL) );
int card;
int deck[40] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10};
card = rand()% deck[40];
printf("%d", card);
return 0;
}
What can I do?
thanks

This simple way to do this is
card = deck[rand() % 40]
This way you are picking a 'random' index in the 40 numbers of your array.
Even tho with this array you have the same probability on every number.You can change the balance by changing the array.

Related

Initializing Arrays with a function

I'm trying to get an array to be initialized in a function. The initialized array should contain a set of random numbers within a certain range when working as intended. Is this possible in C?
#include <time.h> //time func for seed init
#include <stdlib.h> //srand/rand funcs
#include <stdio.h> //Library for printf function
#define LIMIT 100
void myFunction(int array[], int arrayIndexMaxSize){
for (int index = 0; index < arrayIndexMaxSize; index++){
array[index] = rand() % LIMIT; //Where limit is randomly produced from 0 - Limit inclusive
printf("array[%d] of arrayIndexMaxSize(%d) = %d\n", index, arrayIndexMaxSize, array[index]); //prints out th$
}
}
void main(){
srand(time(0)); //initializes kinda random number generator
int array[50]; //If known at runtime or compile time, must be at block scope meaing inside {curly} braces of any$
myFunction(array, 50);
}

Generate random letters in C

here is my code. I am trying generate random alphabet but i see same letters.
example: (YHTGDHFBSHXCHFYFUXZWDYKLXI) How can i fix it? just i need mixed alphabet not same letters. Thank you so much.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void random_string(char * string, unsigned length)
{
/* Seed number for rand() */
srand((unsigned int) time(0));
int i;
for (i = 0; i < length; ++i)
{
string[i] = rand() % 26 + 'A';
}
string[i] = '\0';
}
int main(void)
{
char s[26];
random_string(s, 26);
printf("%s\n", s);
return 0;
}
The operation you are looking for is called a shuffle or a permutation. It is not sufficient to call a random-letter function 26 times, since, as you see, you can generate duplicates.
Instead, start with the string "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and perform a shuffle operation. If you want to learn by doing such things from scratch, I recommend reading about the Fisher-Yates Shuffle then crafting an implementation on your own.
You can do this by having a pool of available characters, and taking one from the pool. Please note that your target string was too short to accomodate the string terminator. Similar to Fisher Yates shuffle.
Edit: changed the types to size_t.
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LENGTH 26 // the cipher key length
void random_string(char * string, size_t length)
{
char pool[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
size_t poolsize = strlen(pool);
size_t index;
size_t i;
srand((unsigned)time(NULL));
for(i = 0; i < length && poolsize > 0; ++i)
{
index = rand() % poolsize; // a random index into the pool
string[i] = pool[index]; // take that character
pool[index] = pool[--poolsize]; // replace it with the last pool ...
} // ... element and shorten the pool
string[i] = '\0';
}
int main(void)
{
char s[LENGTH + 1]; // adequate length
random_string(s, LENGTH);
printf("%s\n", s);
return 0;
}
Program output:
QYMUSFALIZCXGONBJRETHPVKDW
// why program get errors this line? (unused variable cipher_text)
char *cipher_text, msg[255];
You declare cipher_text but you only use msg.
You could declare only:
char msg[255];
Copy paste from cplusplus.com/reference
The pseudo-random number generator is initialized using the argument
passed as seed.
For every different seed value used in a call to srand, the
pseudo-random number generator can be expected to generate a different
succession of results in the subsequent calls to rand.
Two different initializations with the same seed will generate the
same succession of results in subsequent calls to rand.
If seed is set to 1, the generator is reinitialized to its initial
value and produces the same values as before any call to rand or
srand.
In order to generate random-like numbers, srand is usually initialized
to some distinctive runtime value, like the value returned by function
time (declared in header ). This is distinctive enough for most
trivial randomization needs.
/* srand example */
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
int main ()
{
printf ("First number: %d\n", rand()%100);
srand (time(NULL));
printf ("Random number: %d\n", rand()%100);
srand (1);
printf ("Again the first number: %d\n", rand()%100);
return 0;
}

Generating a random, uniformly distributed real number in C [duplicate]

This question already has answers here:
Generate random double number in range [0, 1] in C
(2 answers)
Closed 2 years ago.
I would like to generate a random, real number in the interval [0,1].
I would like to set a pointer, say n, for the number so whenever I stated n, it will be referred to the random generated number.
I have searched on StackOverflow and on Google, but most of them are for C++ or for integers.
I have tried this code suggested to me in the answers:
#include <stdio.h>
#include <stdlib.h>
int main()
{
double n;
double get_random() { return (double)rand() / (double)RAND_MAX; }
n = get_random();
printf("%f", n);
return 0;
}
However, I can only get a value 0.00000000.
How could I fix my program?
You can use:
#include <time.h>
srand(time(NULL)); // randomize seed
double get_random() { return (double)rand() / (double)RAND_MAX; }
n = get_random();
srand() sets the seed which is used by rand to generate pseudo-random numbers. If you don't call srand before your first call to rand, it's as if you had called srand(1) (serves as a default).
If you want to exclude [1] use:
(double)rand() / (double)((unsigned)RAND_MAX + 1);
Full solution:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
double get_random() { return ((double)rand() / (double)RAND_MAX); }
int main()
{
double n = 0;
srand(time(NULL)); // randomize seed
n = get_random(); // call the function to get a different value of n every time
printf("%f\n", n); // print your number
return 0;
}
Every time you run it you will get a different number for n.
This shows how to get random real numbers in the range 0..1 but please note that they are not uniformly distributed. There are only (RAND_MAX+1) discrete values.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
int i;
double n;
srand((unsigned)time(NULL)); // seed the random num generator ONCE only
for(i = 0; i < 3; i++) { // get 3 random numbers
n = (double)rand() / RAND_MAX; // in the range 0 ... 1
printf("%f\n", n); // use correct format specifier for the var
}
return 0;
}
My program output:
0.622608
0.814081
0.878689

Random number in for loop C

I need to generate 1000 random numbers inside a for loop.
my problem is that the random number generated is always the same. since im using time NULL to initiate the generator, why am i getting the same numbers? here is the code i used:
#include <stdio.h>
#include <stdlib.h>
#define LIMIT 30000
int main(){
int i;
srand((long) time(NULL));
for(i = 0; i < 1000; i++){
int x = rand() % LIMIT;
printf("%d\n", x);
}
}
If you run the program multiple times during the same second, you will pass the same value to the generator as seed. You have to wait at least a second before trying it again.
This is because the time function returns the number of seconds since a specific time, and if called multiple times during the same second will return the same value.
your code is right, but you forgot to include the time.h library.
#include <stdio.h>
#include <stdlib.h>
#include <time.h> // <--- now it works
#define LIMIT 30000
int main(){
int i;
srand((long) time(NULL));
for(i=0;i<1000;i++){
int x = rand() % LIMIT;
printf("%d",x);}

How to generate different random numbers in one single runtime?

Consider this code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int ctr;
for(ctr=0;ctr<=10;ctr++)
{
int iSecret;
srand ( time(NULL) );
printf("%d\n",iSecret = rand() % 1000 + 1);
}
}
And it outputs this:
256
256
256
256
256
256
256
256
256
256
Unfortunately, I want the output to print 10 different random numbers in that loop.
Move the call to srand(time(NULL)); to before the for loop.
The problem is that time() changes only once in every second, but you're generating 10 numbers, and unless you have an extremely slow CPU, it won't take a second to generate those 10 random numbers.
So you're re-seeding the generator with the same value each time, making it return the same number.
Put srand ( time(NULL) ); before the loop. Your loop probably runs within a second so you are reinitialising the seed with the same value.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int ctr;
srand ( time(NULL) );
for(ctr=0;ctr<=10;ctr++)
{
int iSecret;
printf("%d\n",iSecret = rand() % 1000 + 1);
}
}
keep srand(time(0)) outside of the for loop.
it should not be inside that loop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int ctr;
srand ( time(NULL) );
for(ctr=0;ctr<=10;ctr++)
{
int iSecret;
printf("%d\n",iSecret = rand() % 1000 + 1);
}
}

Resources