简体   繁体   中英

Uneven Distribution in rand() ( C language )


I wanted to see if there is any way to generate random numbers with uneven distribution which I would like to customize.
for example:
In 100 generated numbers I would like to have :
10 A
20 B
40 C
10 D
15 E
5 F
And I wanted to say that I'm new to programming so try to help me with your simple answers. Really Really Thank You.

This is a pretty simple, yet often highly effective solution for problems like this:

char *samples = "AABBCCDDEEFFFFF";

char randomSample() {
  return sample[rand() % strlen(samples)];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM