Rand function in C

ice_cracked

New Member
Joined
Jul 22, 2016
Messages
17
Office Version
  1. 2016
Platform
  1. Windows
Hi I am trying to write code in C to mimic assembly code and am stuck on getting a random number.

The string I am using is First.Second
which is 12 characters long

The assembly code is as below

mov eax, [esp+78h] ; strlen of characters for the srand call
mov [esp], eax ; Seed value
call _srand

So I am guessing that the value of strlen is being passed to the _srand call
and I am using the below in my C code which seems to work and does not generate any errors

length = strlen(string);
srand(length);

further assembly code is as below

mov eax, [esp+7Ch] ; loop counter
lea edx, ds:0[eax*4] ; loop count = 4-8-C into EDX
mov eax, [esp+74h] ; Pointer to allocated memory which I have created using malloc()
lea ebx, [edx+eax]
call _rand ; first random number call returns 4D 2nd call returns 15FC 3rd call returns 1858 etc.
cdq ; Sign extend EAX into EDX:EAX
idiv dword ptr [esp+78h] ; random number divided by strlen chars entered string
mov ecx, edx ; Save the mod value for later use
mov eax, [esp+78h] ; strlen chars entered string
mov edx, eax ; Count of chars entered string
shr edx, 1Fh ; strlen shr 31 I don't understand what this is for as it always results in = 0
add eax, edx ; result of shr + strlen string so strlen is always unchanged and still 12
sar eax, 1 ; divide strlen string by 2 now = 6
neg eax ; multiply by -1
add eax, ecx ; EAX now 1st time FFFFFFFF 2nd time FFFFFFFA 3rd time FFFFFFFE etc. through the string

these values are then stored in an array created earlier with malloc()

At the moment I am playing with the below C code but it is not working ?

srand(length);

for (i = 0; i < length; ++i) {
myRand = rand();
myBuf += myRand % length - length / 2;



Sorry if this is just a basic question but just starting to learn C and you have to start somewhere.

I appreciate your time in reading this and any suggestions are MOST welcome.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,223,996
Messages
6,175,862
Members
452,676
Latest member
woodyp

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top